10 Replies Latest reply on Feb 10, 2010 7:32 AM by pauland

    EventListener never receiving an event

    bitwyse Level 1

      I have an application that is made up of several mxml files.  In one file I have a comboBox that has a change event as shown below:

       

      <mx:ComboBox id="comboBoxProperties"

           dataProvider="{gXMLCollectionOfProperties}"
           labelField="text" labelFunction="getLabelProperty"
           change="selectProperty( event )"

      />

       

      private function selectProperty( event : Event ) : void
      {
           dispatchEvent( new Event( eventPropertySelect.PROPERTY_SELECT, event.target.selectedLabel ) );
           trace( "You selected: " + event.target.selectedLabel );
      }

       

      In another mxml file I have an event listener to catch the event dispatched above.  For some reason the function in my event listener never get's the event passed to it.  Can anyone see what I'm missing here???????

       

      <mx:VBox
          xmlns:mx="http://www.adobe.com/2006/mxml"
          width="100%" height="100%"
          creationComplete="onComplete()"

      >

       

      import mx.events.*;       
      import com.lms.portal.events.eventPropertySelect;

      public function onComplete():void
      {
           this.addEventListener( eventPropertySelect.PROPERTY_SELECT, updateDataGrid );
      }

       

      Below is the eventPropertySelect.as code

      package com.lms.portal.events
      {
          import flash.events.Event;
         
          public class eventPropertySelect extends Event
          {
              public static const PROPERTY_SELECT : String = "propertySelect";

       

              public var propertyNumber : String = '';
             
              public function eventPropertySelect( type:String, propertyNumber : String = '', bubbles:Boolean=true, cancelable:Boolean=false)
              {
                  super(type, bubbles, cancelable);
                  this.propertyNumber = propertyNumber;
              }
             
          }
      }

        • 1. Re: EventListener never receiving an event
          pauland Level 4

          bitwyse wrote:

           

          I have an application that is made up of several mxml files.  In one file I have a comboBox that has a change event as shown below:

           

          <mx:ComboBox id="comboBoxProperties"

               dataProvider="{gXMLCollectionOfProperties}"
               labelField="text" labelFunction="getLabelProperty"
               change="selectProperty( event )"

          />

           

          private function selectProperty( event : Event ) : void
          {
               dispatchEvent( new Event( eventPropertySelect.PROPERTY_SELECT, event.target.selectedLabel ) );
               trace( "You selected: " + event.target.selectedLabel );
          }

           

          In another mxml file I have an event listener to catch the event dispatched above.  For some reason the function in my event listener never get's the event passed to it.  Can anyone see what I'm missing here???????

           

          <mx:VBox
              xmlns:mx="http://www.adobe.com/2006/mxml"
              width="100%" height="100%"
              creationComplete="onComplete()"

          >

           

          import mx.events.*;       
          import com.lms.portal.events.eventPropertySelect;

          public function onComplete():void
          {
               this.addEventListener( eventPropertySelect.PROPERTY_SELECT, updateDataGrid );
          }

           

          Below is the eventPropertySelect.as code

          package com.lms.portal.events
          {
              import flash.events.Event;
             
              public class eventPropertySelect extends Event
              {
                  public static const PROPERTY_SELECT : String = "propertySelect";

           

                  public var propertyNumber : String = '';
                 
                  public function eventPropertySelect( type:String, propertyNumber : String = '', bubbles:Boolean=true, cancelable:Boolean=false)
                  {
                      super(type, bubbles, cancelable);
                      this.propertyNumber = propertyNumber;
                  }
                 
              }
          }

           

          new Event( eventPropertySelect.PROPERTY_SELECT, event.target.selectedLabel )

           

          Should be

           

          new  eventPropertySelect ( eventPropertySelect.PROPERTY_SELECT, event.target.selectedLabel )

           

          or more likely

           

          new  eventPropertySelect ( eventPropertySelect.PROPERTY_SELECT, event.target.selectedLabel, true )

           

          Whether the listener can catch the event depends on where the event listener is on the display list tree. Can't tell from your bits of code.

           

          Why did you start TWO threads about the same thing?

          • 2. Re: EventListener never receiving an event
            bitwyse Level 1

            I am calling the new Event Constructor as you suggested.

             

            Any idea why my event listener never receives the dispatched event?

            • 3. Re: EventListener never receiving an event
              ShardulSingh Level 3

              Your will be not Listioning event if your Listner Class will be not in the parent root of the event dispatching class.

               

              Pls check it..

               

              http://shardulbartwal.wordpress.com/2009/04/15/managing-custom-events-in-flex-30-in-a-easy -way/

               

              Hope you will find out the solution for the same.

               

               

              with Regards,

               

              Shardul Singh Bartwal

              • 4. Re: EventListener never receiving an event
                bitwyse Level 1

                What do I do if it's not in the parent root path. . .


                How do I force it to get the event???

                • 5. Re: EventListener never receiving an event
                  pauland Level 4

                  Add the listener for your custom event to the stage. Make sure you have bubbling turned on when you dispatch the event.

                   

                  You should make the effort to read the documentation on event bubbling.

                   

                  Paul

                  • 6. Re: EventListener never receiving an event
                    bitwyse Level 1

                    Thanks for the response Paul.  I guess I'm confused.

                     

                    I thoughtthe code below added the event as required???  I have read the doc and I'm afraid it's as clear as mud to me!

                    public function onComplete():void
                    {
                         this.addEventListener( eventPropertySelect.PROPERTY_SELECT, updateDataGrid );

                    }

                    • 7. Re: EventListener never receiving an event
                      Gregory Lafrance Level 6

                      You can add the event listener to the systemManager, though it is not best practice it should work:

                       

                      systemManager.addEventListener( eventPropertySelect.PROPERTY_SELECT, updateDataGrid );

                      When you dispatch the event, the second are should be true, as in this default constructor definition for the Event class:

                       

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

                       

                      So for you:

                       
                      dispatchEvent( new Event( eventPropertySelect.PROPERTY_SELECT, event.target.selectedLabel ), true );

                       

                      If this post answers your question or helps, please mark it as such.


                      Greg Lafrance - Flex 2 and 3 ACE certified

                      www.ChikaraDev.com

                      Flex / AIR Development, Training, and Support Services

                      • 8. Re: EventListener never receiving an event
                        pauland Level 4

                        bitwyse wrote:

                         

                        Thanks for the response Paul.  I guess I'm confused.

                         

                        I thoughtthe code below added the event as required???  I have read the doc and I'm afraid it's as clear as mud to me!

                        public function onComplete():void
                        {
                             this.addEventListener( eventPropertySelect.PROPERTY_SELECT, updateDataGrid );

                        }

                         

                         

                        The user interface is composed of a lot of seperate parts all connecting together - parent to child as a tree. This is known collectively as the displayList. When you add a component it sits as one of the leaves on the tree and it ha a parent that contains that component. Just like a real tree the display list has many branches. If your component sits on one branch and the listener on another, the listener will never be able to receive the event because events move up the branches away from the leaves and  towards the trunk. A listener on a seperate branch can't see the event.

                         

                        This is why you have had a lot of advice about where to add the event listener - it must sit closer to the root of the tree than the component that dispatches the event and it must also be on the same branch.

                         

                        Please check out online tutorials on event propogation with actionscript 3.

                         

                        Your code snippet looks good, but offers no information because everything depends on the relationship between "this" and the object that dispatches the event. gregs suggestion effectively puts the listener in a place that will ensure that the event is caught (provided it bubbles).

                         

                        You will have to get the hang of events and bubbling to be succesful with actionscript.

                         

                        Paul

                        • 9. Re: EventListener never receiving an event
                          Gregory Lafrance Level 6

                          To expand on this, each component in your Flex app is somewhere in the display list. So events are dispatched and travel from the top of the display list, through each container down until it reaches the target component that dispatched the event.

                           

                          So you can't listen to an event if it is not in the display list of the target. That's why listening in the systemManager (which is near the top of the display list, is often a solution.

                           

                          If this post answers your question or helps, please mark it as such.


                          Greg Lafrance - Flex 2 and 3 ACE certified

                          www.ChikaraDev.com

                          Flex / AIR Development, Training, and Support Services

                          • 10. Re: EventListener never receiving an event
                            bitwyse Level 1

                            While I have been working with Flex for a couple of years now, I have primarily been using Cairngorm and have had little to do with events on my own.  I agree with you Paul, I need to learn more about events.

                             

                            The good news is the adding the event listener to the system manager worked great.  Thank you both for your suggestions.  I appreciate all that you guys do in these forums.