Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
370 views
in Technique[技术] by (71.8m points)

actionscript 3 - What is the difference between target and currenttarget in flex?

Can any one please tell me the difference between target and currenttarget in flex?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Sure, I've had some trouble with this too. The currentTarget property is the IEventListener you registered the event handler for. The target is the one that dispatched the event that you are currently handling. So the currentTarget changes, the target doesn't.

Check out the following example:

Sample App

<?xml version="1.0" encoding="utf-8"?>
<mx:Application
    xmlns:mx="http://www.adobe.com/2006/mxml"
    creationComplete="addListeners()">

    <mx:Script>
        <![CDATA[

            protected function addListeners():void
            {
                greatGrandParent.addEventListener(Event.COMPLETE, completeHandler);
                grandParent.addEventListener(Event.COMPLETE, completeHandler);
                aParent.addEventListener(Event.COMPLETE, completeHandler);
                child.addEventListener(Event.COMPLETE, completeHandler);
                // dispatch event that "bubbles", second param is "true"
                // dispatched from child
                child.dispatchEvent(new Event(Event.COMPLETE, true));
            }

            protected function completeHandler(event:Event):void
            {
                trace("target: ", event.target + ", currentTarget: ", event.currentTarget);
            }

        ]]>
    </mx:Script>

    <mx:Panel id="greatGrandParent">
        <mx:Panel id="grandParent">
            <mx:Panel id="aParent">
                <mx:Button id="child"/>
            </mx:Panel>
        </mx:Panel>
    </mx:Panel>

</mx:Application>

Output

target:  MyApp.greatGrandParent.grandParent.aParent.child, currentTarget:  MyApp.greatGrandParent.grandParent.aParent.child
target:  MyApp.greatGrandParent.grandParent.aParent.child, currentTarget:  MyApp.greatGrandParent.grandParent.aParent
target:  MyApp.greatGrandParent.grandParent.aParent.child, currentTarget:  MyApp.greatGrandParent.grandParent
target:  MyApp.greatGrandParent.grandParent.aParent.child, currentTarget:  MyApp.greatGrandParent

It's a simple tree of display objects, and when the app is ready I:

  1. Add listeners for the same event on each component in the tree.
  2. Dispatch an arbitrary event (just for demonstration). I chose Event.COMPLETE.

Since everything has registered an eventHandler for that same event, and since I have set bubbles to true (new Event(type, bubbles)), anything in the tree, from child to greatGrandParent and beyond, that has registered an event handler for Event.COMPLETE, will run that method: completeHandler. Events travel up the chain then back down. The target is the one that dispatched the event, so since child dispatched it, it should be constant. The currentTarget is what changes.

This means that, say you want to check when you are rolling over a DataGrid in Flex, you want to know when you roll over a Checkbox inside one of the itemRenderers in the DataGrid. One way is to addEventListener on every itemRenderer's checkbox for MouseEvent.ROLL_OVER. Another way is to addEventListener to the DataGrid itself for MouseEvent.ROLL_OVER, and check what the target is on the event:

protected function dataGrid_rollOverHandler(event:MouseEvent):void
{
    // event.currentTarget is DataGrid
    if (event.target is CheckBox)
        trace("rolled over checkbox!");
}

That's how I often use event.target.

Hope that helps, Lance


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...