Events in Flash player
In earlier version of ActionScript (Prior to AS 3.0) Event flows didn’t exist and event listeners could only be added to the Objects that dispatched/generated events.
In AS 3.0 listeners could be added to the any object/node in the event flow.
Event Flow: (How an event object moves through the display list.)
Stage: It stays at the top of the tree (display list), its special object container, can only be accessed through a display object. Every display object has a property named stage that refers to the Stage for that application.
Parent Node: As name suggests it’s the Parent of the Child Node(s).
Child Node (Target Node): It’s the node that dispatches events.
The currentTarget property refers to the current node that is being examined for event listeners.
Event Flow Stages/Phases:
Capturing: This phase comprises all of the nodes from the Stage to the parent of the target node.
Targeting: This consists solely of the target node.
Bubbling: This comprises the nodes encountered on the return trip from the parent of the target node back to the Stage.
Advanced Event Flow Information:
When a faceless event dispatcher, such as a Validator, dispatches an event, there is only a targetingphase, because there is no visual display list for the Event object to capture or bubble through.
Event that have bubbling phase activated by default are MouseEvent, KeyBoardEvent.
Capturing Phase: By default, no container listens during the capturing phase. The default value of the use_capture argument is false. The only way to add a listener during this phase is to pass true for the use_capture argument when calling the addEventListener() method, as the following example shows:
myPanel.addEventListener(MouseEvent.MOUSE_DOWN, clickHandler, true);
Event listener inline with MXML, Flex sets this argument to false; you cannot override it.
Note: Capturing is computationally intensive.
For Bubbling and Capturing phase together:
If you want your event to traverse both the capturing and bubbling phases, you must call addEventListener() twice: once with use_capture set to true, and then again with use_capture set to false.
If you create a custom ActionScript class that dispatches its own events but does not extend UIComponent, you can extend the flash.events.EventDispatcher class to get access to the addEventListener(), removeEventListener(), and dispatchEvent() methods.
The difference between
willTrigger() is that
hasEventListener() examines only the object to which it belongs, whereas
willTrigger() examines the entire event flow for the event specified by the type parameter.
Event Order: Why do we have Two Models (Capturing and Bubbling)
“If an element and one of its ancestors have an event handler for the same event, which one should fire first?”
Answer: This depends on the browser, as Netscape and Microsoft had their own interpretation of how the dispatched event should be handled.
These disparity lead to this:
- Netscape said that the event on element1 takes place first. This is called event capturing.
- Microsoft maintained that the event on element2 takes precedence. This is called event bubbling.
| element1 |
| ————————- |
| |element2 | |
| ————————- |
Enter W3C Model
With the commandment that “Any event taking place in the W3C event model is first captured until it reaches the target element and then bubbles up again.”