8 Replies Latest reply on Apr 30, 2009 9:32 PM by Flex harUI

    Get the origin of dispatched Event

    drillnaut Level 1

      Hello Everyone,

        I am using a custom event for a class that I have made.  In the main class , I add a listener to the aforementioned objects of the class.  When the event is fired and caught, I need to know exactly which one of my objects has dispatched the event.  I have tried casting the event to the class

       

      list:Mylist = event.currentTarget as Mylist.

       

      However , flex builder won't let that slide.  Any help is greatly appreciated.

       

      Sincerely,

        JD

        • 1. Re: Get the origin of dispatched Event
          Michael Borbor Level 4

          Hi. What's the compiler error?

          • 2. Re: Get the origin of dispatched Event
            Gregory Lafrance Level 6

            Yeah, I'm wondering what error you're getting as well.

             

            list:Mylist = event.currentTarget as Mylist

             

            So how about the following:

             

            list:Mylist = Mylist(event.currentTarget)

            list:Mylist = event.target as Mylist

            list:Mylist = Mylist(event.target)

             

            Just trying to come up with possibilities, but can't really say without the error.

             

            If you just wanted to know which object dispatched the event, if you gave the object a "name" and checked that property, that might do the trick.

            1 person found this helpful
            • 3. Re: Get the origin of dispatched Event
              Barna Biro Level 3

              Hi there,

               

              I think that the only way you can know which project / component triggered the event is to pass along this information when you dispatch the event. I don't think there's a built in something that can help you out with this ( it's really hard to keep track of events, know where they are heading, how's listening for them and where they are comming from ).

               

              With best regards,

              Barna Biro

              1 person found this helpful
              • 4. Re: Get the origin of dispatched Event
                Gregory Lafrance Level 6

                Yeah, because you are already using a custom event, you could have a string static variable to identify the class, or else a non-static string variable referencing another variable, this time an int, that is static you increment in the constructor and concatenate with the non-static variable to idenitfy the instance, then in the event handler, access that custom event property, and you know the class and the instance.

                • 5. Re: Get the origin of dispatched Event
                  drillnaut Level 1

                  Ok folks, after some research , I believe that I have found the solution.  Before I was using simple custom events i.e , dispatchEvent(new Event("dispatchMe")).  However, I think that the better solution is to implement a custom event class which can store the dispatching object as a member variable.  Something like this which I found on the web.

                   

                  package.events
                  {
                   
                        public class CustomEvent extends Event
                        {
                   
                             public static const CUSTOM_TYPE:String = "customType";
                   
                             public var customProp:String;
                   
                             public function CustomEvent( type:String, cp:String, bubbles:Boolean, cancelable:Boolean )
                             {
                                  super( type, bubbles, cancelable )
                   
                                  customProp = cp;
                             }
                   
                        }
                   
                  }

                   

                  Thank you very much for your help and assistance !

                  • 6. Re: Get the origin of dispatched Event
                    drillnaut Level 1

                    That did the trick, thanks

                    • 7. Re: Get the origin of dispatched Event
                      ntsiii Level 3

                      FYI, event.currentTarget.className will return the class of the dispatching object.  If the instance has an id, you can also access that.

                       

                      Tracy

                      • 8. Re: Get the origin of dispatched Event
                        Flex harUI Adobe Employee

                        A nit:  event.currentTarget is probably the dispatching object in this case, but won't always be for bubbling and capture phase handlers.

                         

                        Alex Harui

                        Flex SDK Developer

                        Adobe Systems Inc.

                        Blog: http://blogs.adobe.com/aharui