Follow Flex After Dark on Twitter

Event Dispatcher (flash.events.IEventDispatcher)

Flex is driven by events. Event Dispatchers are the key for coordinating the announcement of events and notifying appropriate listeners.

An Event Dispatcher will implement the IEventDispatcher interface and optionally extend the base EventDispatcher class, both defined in the flash.events package.

The IEventDispatcher interface defines several functions, but the two key methods are:

  • dispatchEvent() - Dispatches an event into the event flow so that registered listeners are notified
  • addEventListener() - Registers an event listener object so that the listener receives notification of dispatched event

All Flex controls in the mx.controls package (e.g. Button, TextArea, DataGrid) and Flex containers in the mx.containers package (e.g. Canvas, VBox) implement the IEventDispatcher interface and are therefore capable of registering listeners and dispatching events.

Event Lifecycle: Listening, Dispatching, and Handling

Important event participants:

  • dispatcher - the subject of the event
  • event - the event object encapsulating details of the event occurence
  • listener - the handler function for dealing with an event

Listening for Events

Listening for an event involves registering a callback or handler function with an event dispatcher.

   // register a handler function as an event listener
   dispatcher.addEventListener( "eventType", handlerFunction );

Dispatching Events

Dispatching an event happens when something interesting occurs that an event dispatcher wants to notify listeners of.

   // dispatch an event to my listeners
   dispatchEvent( new Event( "eventType" ) );

Handling Events

Handling and event is the result of an event dispatcher calling a callback or handler function of a listener in response to an event.

   // handle an event in a listener
   public function handlerFunction( event:Event ):void
   {
      ...
   }

Related Links:

Related Docs
  • Flex Events Events drive Flex applications. See how to register listeners and dispatch events.
Recent Docs
Random Docs