<mx:Button id="myButton2" label="test" click="myButton_click(event)"/> |
myButton.addEventListener(MouseEvent.Click, myButton_click); |
- Capturing
- Targeting
- Bubbling
myButton.addEventListener(MouseEvent.CLICK, myButton_click, true); |
During the BubblingPhase the displayList is walked in reverse and the system is looking for eventListeners for this event with the useCapture set to false (the default value). When they are found, those eventListeners are executed.
During both Capturing and Bubbling the event object has two variables that are important. One is target and the other is currentTarget. Target is always the object that initiated the event. The currentTarget is always the current item in the displayList being processed. You should always use currentTarget in your eventListener functions. That will always be the object that the eventListener is associated to. For example, if the user clicks on the text of the button. The target will be set to a label class since that is the sub component that was truly clicked on and initiated the event process. But when your eventListener function, attached to the button, is executed the currentTarget will be the button and the target will still be the label.
At any point in the Capturing or Bubbling process you can stop the process from continuing to the next item in the display list. There are two options, stopPropagation and stopImmediatePropagation. The difference between the two functions is that stopPropagation will allow other items at the same level in the displayList to receive the event, where the stopImmediatePropagation function stops right then.
Understanding the event propagation within Flex provides many options. You can control what components get to see events. You can change the behavior of items, mostly done when you are building custom components. You can reduce code by putting shared code in a common click function at a higher level. And, of course, the more you understand about how components interact the better custom components you can build.
3 comments:
I would not recommend writing an application where all the event handling is at the top most level. It all sounds useful and handy, trying doing it on a project with 20+ developers and very soon things will be intermingled.
@Anonymous
I could not agree with you more and concidered not even putting it in the post. Only reason I did is most of all of the Adobe documentation uses that example.
Thanks
Jeff
I still don't construe the bubbling and capturing phase, why do we need this????
Any advantage in using capturing phase...
Post a Comment