User Guide
Using listeners to handle events 69
Using listener functions
Unlike the handleEvent syntax, several listener functions can handle different events. So
instead of having the
if and else if checks in myHandler, you can just define
myChangeHandler for the change event and myScrollHandler for the scroll event and
register them, as shown here:
myList.addEventListener("change", myChangeHandler);
myList.addEventListener("scroll", myScrollHandler);
To use a listener function, you must first define a function:
function myFunction:Function(evtObj:Object){
// your code here
}
Then you call the addEventListener() method from the component instance that
broadcasts the event. The
addEventListener() method takes two parameters: a string
indicating the name of the event and a reference to the function.
componentInstance.addEventListener("eventName", myFunction);
You can call addEventListener() from any component instance; it is included in every UI
component from the EventDispatcher class. For more information, see
EventDispatcher.addEventListener().
For information about the events a component broadcasts, see each component’s entry in the
Components Language Reference.
TIP
The evtObj parameter is an object that is automatically generated when an event is
triggered and passed to the function. The event object has properties that contain
information about the event. For details, see “About the event object” on page 77.