
114 Events
If you try to call another method on the target (for example, the getStyle() method), Flex
returns an error. The
getStyle() method is a method of UIComponent, a subclass of
DisplayObject. Therefore, you must cast event.target to UIComponent before calling the
getStyle() method, as the following example shows:
function myEventListener(e:Event) {
UIComponent(e.target).getStyle("color");
}
If you use the target property of the Event object to determine which control triggered the
event, you should consider changing the
target property to currentTarget. The
currentTarget property supports event bubbling and capturing, which are new in Flex 2.
During these phases, the event is actually being handled by the target’s parent containers. The
target still refers to the dispatcher of the event, but the
currentTarget refers to whatever
control is currently processing a bubbling or capture event.
For example, if you have
<mx:List click="..."/>, inside the click handler event.target
might be one of the rows, but
event.currentTarget is a reference to the List control, which
is generally what you would expect.
Another example is what occurs when you assign a mouseDown handler on a TextInput
control. Actually, the TextField class that is inside the TextInput control dispatches the event
(depending on where the user clicked), so the TextField control, not the TextInput control, is
the
target. However, the TextInput control is the currentTarget if that is where you
attached the handler.
Most controls have internal subcomponents that are often the target for mouse events.
Using static constants
Use static constants to represent event types; for example, use MouseEvent.CLICK rather than
"click", as the following example shows:
addEventListener("click", myClickListener); // Flex 1.5
addEventListener(MouseEvent.CLICK, myClickListener); // Flex 2
Change the following:
switch (event.type) {
case "click":
...
}
To this :
switch (event.type) {
case MouseEvent.CLICK:
...
}
Comentários a estes Manuais