Flex 101: Event “target”/”currentTarget

Flex 3/ActionScript 3 follows the 3C DOM event model convention, using the terms “target” and “currentTarget” to describe source of the event and the object who registers its interest in the related events. If a button or its parent container registers interest in a mouse click event, either the button or its container becomes “currentTarget” while the button is the “target” at bubbling propagation.

In Flex/Flash, things like SystemManager (which is instantiated by Application and which inherits from flash.display.MovieClip) can also register its interest in any UIComponent(Note that stylized skins that are drawn by the drawing APIs will not trigger any MouseEvent event if the systemManger registers MouseEvent), which as Matt says will “broadcast” the event(i.e., call the event handlers). In this case, “currentTarget” is the systemManager and “target” can be the visual omponent or the loader of the visual content “under” the stage who is the source of the event.

The same event handler for the same UIComponent can have different event targets for different event types. For example, in the following code, the same function handles “click”/”change”/”close” events that are registered by a comboBox. For a “click” event, the target is never the comboBox but either UITextField or Button depending where you click on the comboBox. This seems to make sense since the client would presumably be more interested in which part of the comboBox a user clicks for a “click” event.

<mx:ComboBox close=”changeCompState(event);” change=”changeCompState(event);” click=”changeCompState(event);” />
//click : componentStateTest1_0._componentStateTest1_ComboBox1.TextInput8.UITextField10
//change : componentStateTest1_0._componentStateTest1_ComboBox1
//close : componentStateTest1_0._componentStateTest1_ComboBox1
//click : componentStateTest1_0._componentStateTest1_ComboBox1.Button6
//change : componentStateTest1_0._componentStateTest1_ComboBox1
//close : componentStateTest1_0._componentStateTest1_ComboBox1
private function changeCompState(event:Event):void
trace(event.type+” : “+event.target);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: