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
  • ActionScript EventDispatcher EventDispatcher is used for event dispatching and listener registration.
  • Flex DataGrid The Flex DataGrid control is used for displaying tabular data.
  • ActionScript E4X E4X (ECMAScript for XML) enables ActionScript to handle XML as a first-class citizen.
  • Flex Embedding Assets Overview of embedding assets in Flex applications.
  • Flex List Controls Flex List controls refer to a special group of controls that display collections of items, usually from a data provider.
Random Docs
  • Flex Forms Forms in Flex can be used for collecting, and validating, user input.
  • ActionScript Get/Set Functions ActionScript Get/Set Functions are special function handlers for property accessors.
  • Flex HTML Text Text Controls support the htmlText property.
  • MXML Anatomy The anatomy of an MXML component file.
  • Flex StyleSheets StyleSheets define styles for Flex components and can be defined inline or loaded at run-time.