User Guide

64 Handling Component Events
Using listeners to handle events
The version 2 component architecture has a broadcaster/listener event model. (A broadcaster is
sometimes also referred to as a dispatcher.) It is important to understand the following key
points about the model:
All events are broadcast by an instance of a component class. (The component instance is
the broadcaster.)
A listener can be a function or an object. If the listener is an object, it must have a callback
function defined on it. The listener handles the event; this means the callback function is
executed when the event occurs.
To register a listener to a broadcaster, call the addEventListener() method from the
broadcaster. Use the following syntax:
componentInstance.addEventListener("eventName",
listenerObjectORFunction);
You can register multiple listeners to one component instance.
myButton.addEventListener("click", listener1);
myButton.addEventListener("click", listener2);
You can register one listener to multiple component instances.
myButton.addEventListener("click", listener1);
myButton2.addEventListener("click", listener1);
The handler function is passed an event object.
You can use the event object in the body of the function to retrieve information about the
event type, and the instance that broadcast the event. See About the event object
on page 77.
A listener object remains active until explicitly removed using
EventDispatcher.removeEventListener(). For example:
myComponent.removeEventListener(“change”, ListenerObj);