The V2 Component Event Model Part 2- UIEventDispatcher
October 09, 2003

In part 1 of this series on the V2 component event model, I deconstructed EventDispatcher, which is the class that implements the base event handling for all components. In this segment, I'll attempt to do the same with UIEventDispatcher.

UIEventDispatcher (mx.core.events.UIEventDispatcher) extends EventDispatcher to add functionality for keyboard events and load events. As far as I can tell, V2 components always implement event handling via UIEventDispatcher, not EventDispatcher directly. They do so by calling UIEventDispatcher.initialize on UIObject (which is the base for all V2 components), thus adding the ability for the component to add/remove event listeners and dispatch events. For more details, see my previous entry on "insights into component architecture".

So now that you know a bit about what it does, let's look under the hood and see how it does it. As I just mentioned, UIEventDispatcher extends EventDispatcher and in doing so, it pretty much overrides all of EventDispatcher's public methods (initialize, addEventListener, removeEventListener, dispatchEvent). However, it still defers most of the work to it's superclass, but adds some nice functionality to support key and load events. There are 3 important objects used in UIEventDispatcher that you need to be aware of:

<br />
static var keyEvents:Object = { keyDown: 1, keyUp: 1 };<br />
static var loadEvents:Object = { load: 1, unload: 1};<br />
static var lowLevelEvents:Object = { keyEvents: ["addKeyEvents", "removeKeyEvents"], loadEvents: ["addLoadEvents", "removeLoadEvents"] };<br />

When you add a listener (using addEventListener) via UIEventDispatcher, what happens is the following:

1) It registers the listener by calling addEventListener on it's superclass.

2) It looks through the properties in the "lowLevelEvents" object. In doing so, it extracts the values of the properties (ie., "keyEvents" or "loadEvents") and uses those to look through the object in the class with the same name. The reason it does this is to try to locate the name event that is being registered. In this case, there are really 4 important events:

<br />
keyDown<br />
keyUp<br />
load<br />
unload<br />

These are listed in keyEvents and loadEvents objects. So in 20 words or less, it basically goes through this process to make sure that the event that is being listened for actually exists in the event dispatcher.

3) If an event match is found, it then looks back in the lowLevelEvents object and runs a function on itself with the same name as the 0th index in the array with the same name as the object that contained the name of the event. Now that was a mouthful. Let's try an example to illustrate what is happening here...

<br />
import mx.events.UIEventDispatcher;</p>

<p>var dispatchingObj:Object = {};<br />
var listeningObj:Object = {};<br />
UIEventDispatcher.initialize(dispatchingObj);<br />
dispatchingObj.addEventListener("keyDown",listeningObj);<br />

When "addEventListener" is run in this code, what happens is:
a)

Posted by philter at October 9, 2003 08:42 PM

Comments Disabled