Writing the component’s ActionScript code
15
You add these keywords anywhere inside the class definition. For more information on
embedding assets, see
Developing Flex Applications
.
To create a SWF file from which you embed assets, create a new FLA file and insert new symbols.
For each symbol, select Export for ActionScript or place the symbols on the stage before saving
the file as a SWF file.
Handling events
The event model is a dispatcher-listener model based on the DOM Level 3 proposal for event
architectures. Every component in the architecture emits events in a standard format, as defined
by the convention. Those events vary across components, depending on the functionality that the
component provides.
Components generate and dispatch events and consume (listen to) other events. An object that
wants to know about another object’s events registers with that object. When an event occurs, the
object dispatches the event to all registered listeners by calling a method requested during
registration. To receive multiple events from the same object, you must register for each event.
Although every component can define unique events, events are inherited from the core classes of
the architecture, mx.core.UIObject and mx.core.UIComponent. These classes define low-level
component events, such as
draw
,
resize
,
move
,
load
, and others that are fundamental to all
components. Subclasses of these classes inherit and broadcast these events.
Dispatching events
In the body of your component’s ActionScript class file, you broadcast events using the
dispatchEvent()
method. The
dispatchEvent()
method has the following signature:
dispatchEvent(
eventObj
)
The
eventObj
argument is the event object that describes the event. You can explicitly build an
event object before dispatching the event, as the following example shows:
var eventObj = new Object();
eventObj.type = "myEvent";
dispatchEvent(eventObj);
You can also use the following shortcut syntax that sets the value of the
type
property for the
event object and dispatches the event in a single line:
dispatchEvent({type:"myEvent"});
The event object has an implicit property,
target
, that is a reference to the object that triggered
the event.
For each event that your custom component dispatches, you add an
Event
metadata keyword
defining that event; for example:
[Event("myEvent")]
You add these keywords immediately before the class definition. If you do not identify an event in
the class file with the
Event
metadata keyword, the compiler ignores the event during
compilation, and Flex ignores this event triggered by the component during runtime.
Summary of Contents for FLEX-CREATING ADVANCED COMPONENTS
Page 1: ...Creating Advanced Components...
Page 4: ...4 Contents...