User Guide
142 Creating Simple Visual Components in ActionScript
Your custom component can also define event listeners within the component itself to handle
the events internally. For example, “Defining public properties as variables” on page 127
defined event listeners for the
keyDown and creationComplete events within the body of the
component. This allows the component to handle those events internally.
The example used the
creationComplete event to access the default fontSize property of
the component. You could not access this property in the constructor itself because Flex does
not define it until after the component is created. For more information on the initialization
order of a component, see Chapter 10, “Creating Advanced Visual Components in
ActionScript,” on page 147.
Dispatching custom events
Your ActionScript component can define custom events and use the predefined events. You
use custom events to support data binding, to respond to user interactions, or to trigger
actions by your component. For an example that uses events to support data binding, see
“Using data binding with custom properties” on page 132.
For each custom event dispatched by your component, you must do the following:
1. Create an Event object describing the event.
2. (Optional) use the [Event] metadata tag to make the event public so that other
components can listen for it.
3. Dispatch the event by using the dispatchEvent() method.
To add information to the event object, you define a subclass of the flash.events.Event class to
represent the event object. For more information on creating custom event classes, see
Chapter 4, “Creating Custom Events,” on page 35.
NOTE
Even though you define event listeners for the events in the component itself, your
application can also register listeners for those events. The event listeners defined within
the component execute before any listeners defined in the application.