4 Replies Latest reply on Oct 11, 2011 8:54 AM by ozDiGennaro

    Dispatching & listening for custom events from custom component [Flex 4.1]

    martinjconnolly

      I'm giving this a try for the first time and I'm not sure I have the recipe correct!

      I have a custom component - it contains a data grid where I want to double click a row and dispatch an event that a row has been chosen.

      I created a custom event

       

      package oss

      {

          import flash.events.Event;

         

          public class PersonChosenEvent extends Event

          {

              public function PersonChosenEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false)

              {

                  super(type, bubbles, cancelable);

              }

              // Define static constant.

              public static const PERSON_CHOSEN:String = "personChosen";

             

              // Define a public variable to hold the state of the enable property.

              public var isEnabled:Boolean;

             

              // Override the inherited clone() method.

              override public function clone():Event {

                  return new PersonChosenEvent(type);

              }

          }

      }

       

      Then I try to dispatch the event within the component when the datagrid is doubleclicked:

      import oss.PersonChosenEvent

      ....

      dispatchEvent(new PersonChosenEvent(PersonChosenEvent.PERSON_CHOSEN, true, false));

       

      And in the parent application containing the component I do on creationComplete

       

      addEventListener(PersonChosenEvent.PERSON_CHOSEN,addPersonToList);

       

      The event does not seem to fire though. And if I try to evaluate the "new PersonChosenEvent(..." code it tells me "no such variable".

       

      What am I doing wrong?

       

       

      (It was so easy in VisualAge for Java, what have we done in the last 10 years?? )

       

      Martin

        • 1. Re: Dispatching & listening for custom events from custom component [Flex 4.1]
          Claudiu Ursica Level 4

          I think your clone does not passes the bubbling as true. So you kind of need to do that yourself ...

           

           

          C

          • 2. Re: Dispatching & listening for custom events from custom component [Flex 4.1]
            UbuntuPenguin Level 4

            (It was so easy in VisualAge for Java, what have we done in the last 10 years?? )

             

            ಠ_ಠ

            • 3. Re: Dispatching & listening for custom events from custom component [Flex 4.1]
              martinjconnolly Level 1

              I meant creating and promoting events was easy in VA Java. The IDE took care of it. Perhaps Flash Builder should do something similar. All this hand-coding makes me cranky....

              • 4. Re: Dispatching & listening for custom events from custom component [Flex 4.1]
                ozDiGennaro Level 1

                I've done this kind of thing routinely, when I want to add information to the event.  I never code the "clone" method at all.

                Be sure that you are listening to the event on a parent of the dispatching component.

                You can also have the dispatching component listen for the event too, and use trace() to get a debug message.

                I doubt if it has anything to to with "bubbles" since the default is true.

                 

                Sample code

                 

                In a child (BorderContainer)

                dispatchEvent(new ActivationEvent(ActivationEvent.CREATION_COMPLETE,null,window));

                ...

                 

                In the container parent (BorderContainer)

                activation.addEventListener(ActivationEvent.CREATION_COMPLETE,activationEvent);

                 

                 

                package components.events

                {

                    import components.containers.SemanticWindow;

                    import components.triples.SemanticActivation;

                 

                    import flash.events.Event;

                   

                    public class ActivationEvent extends Event

                    {

                        public static const LOADED:String = "ActivationEvent: loaded";

                        public static const CREATION_COMPLETE:String = "ActivationEvent: creation complete";

                        public static const RELOADED:String = "ActivationEvent: reloaded";

                       

                        public static const LEFT_SIDE:String = "ActivationEvent: left side";

                        public static const RIGHT_SIDE:String = "ActivationEvent: right side";

                       

                        private var _activation:SemanticActivation;

                        private var _window:SemanticWindow;

                       

                        public function ActivationEvent(type:String, activation:SemanticActivation, window:SemanticWindow)

                        {

                            super(type);

                            _activation = activation;

                            _window = window

                        }

                       

                        public function get activation():SemanticActivation {

                            return _activation;

                        }

                       

                        public function get window():SemanticWindow{

                            return _window;

                        }

                    }

                }