3 Replies Latest reply on Oct 19, 2010 10:40 PM by kiran7881

    target and CurrentTarget

    kiran7881 Level 1

      Hi ,

       

      while going through the diference between target and CurrentTarget .

       

      I have read that dont use target as they may refer to subclass of the Event Disptacher .
      For example when you click on a Button Label  they might give their subclass

       

      But to achive the above  i tried number of times , but i coudn't find any of such thing .

       

      for example

       

      <mx:script>

       

      public function Call(event:Event):void
      {

       

      Alert.show(event.target.id.toString()) ;
      }
      </mx:script>

       


      <mx:Button label="Flex" id="Flex" click=Call(event)/>

       

      Doing so ,  It gives me the correct thing that is Flex , please tell

       

      me then why is it mentioned as such that
      dont use target as they may refer to subclass of the Event Disptacher

       

      .

       

      Thanks in advance .

        • 1. Re: target and CurrentTarget properties of Event
          kiran7881 Level 1

          Hi any help please .

          • 2. Re: target and CurrentTarget properties of Event
            BhaskerChari Level 4

            Hi Kiran,

             

            In Flex, an event object is created whenever an event is dispatched. The event object has two important properties – event.target and event.currentTarget. The event.target property references the event dispatcher while event.currentTarget property refers to the node which is currently being checked for event listeners. While event.target property stays the same throughout the event flow (capturing, targeting and bubbling phases), the event.currentTarget property changes on each node.


            For example, if an event is fired from a TextInput component placed inside a Panel, event.target in this case will be TextInput throughout the event flow while event.currentTarget will vary from Application to Panel to TextInput as the event object goes up or down the flow chain.


            An important point to note over here is that when an event is fired from a component, event.target property does not necessarily reference the component which dispatched the event. It may refer to a subcomponent which is part of that component. This means that if you click on a button’s label, the event object’s event.target property won’t be button (as you might have hoped) but will be a subcomponent (within the button class) – UITextField (renders label in a button).This is a very important point while handling events. In this case, if the button component was listening for the click event, the event handler will be called if you are using event.currentTarget property since (sooner or later) event.currentTarget will refer to the button component as the mouse event bubbles through the chain. But, if you were using event.target property, the event handler will never get called as a subcomponent (UITextField) fired the event and is referred to in event.target property. Hence, you should always use event.currentTarget property instead of event.target.

             

             

            For more details refer the link below:

            http://jvalentino.blogspot.com/2009/05/flex-eventtarget-versus.html

             

            Thanks,

            Bhasker

            • 3. Re: target and CurrentTarget properties of Event
              kiran7881 Level 1

              Thanks Bhaskar ,

               

              This means that if you click on a button’s label, the event object’s event.target property won’t be button (as you might have hoped) but will be a subcomponent (within the button class) – UITextField (renders label in a button).

               

              How is it possible to achive this scenario , because it always refered to me Button only when i used event.target