5 Replies Latest reply on Jul 9, 2010 12:33 AM by Sebastien V.

    Issue with AddEventListener

    lkenney2 Level 1

      I have one class classed ContentSelector.as:

       

      package com.ascentify.components.content.events
      {
           import com.ascentify.components.content.variables.AscentifyVariables;
           
           import flash.events.EventDispatcher;
           
           public class ContentSelector extends EventDispatcher
           {
                public var data:XML;
                
                public function ContentSelector(section:String)
                {
                     switch (section) {
                          case "story":
                               data = AscentifyVariables.instance.storyContent;
                               break;
                          case "listenRepeat":
                               data = AscentifyVariables.instance.listenAndRepeatContent;
                               break;
                          case "listenType":
                               data = AscentifyVariables.instance.listenAndTypeContent;
                               break;
                          case "listenAnswer":
                               data = AscentifyVariables.instance.listenAndAnswerContent;
                               break;
                          default:
                               throw new Error("There is no section with this name.");
                     }
                     dispatchEvent(new Event("ContentSelector.CONTENT_SECTION"));
                }
      
           }
      }
      

       

      Another class, LoadContent, contains an instance of that class:

       

      ...
      
                              _contentSelector = new ContentSelector("story");
                     _contentSelector.addEventListener("ContentSelector.CONTENT_SECTION", onContentSelectorComplete);
                
                }
                
                public function onContentSelectorComplete(e:Event):void
                {
                     onContentLoaded(_contentSelector.data);
                }
      
      ...
      

       

      Using a couple of Alerts, I have confirmed the story section is chosen and the data is there. However, the dispatchEvent appears to have a problem, as the eventListener is never fired. Any ideas?

        • 1. Re: Issue with AddEventListener
          UbuntuPenguin Level 4

          Your execution probably goes something like this.  ( Remember , the thread you have shown is "blocking" ).

           

          Here the content selector is being constructed.  Generally , you don't want to do a whole lot in your constructor functions.  This is an example of why not.

          _contentSelector = new ContentSelector("story");

          At the end of that function , an event is dispatched , announcing to the world that something about a ContentSelector has changed !

          dispatchEvent(new Event("ContentSelector.CONTENT_SECTION"));

          Now we move up out of scope of the _contentSelector to add the event listener .... AFTER the event has been fired.

           _contentSelector.addEventListener("ContentSelector.CONTENT_SECTION", onContentSelectorComplete);

           

           

          If this post was helpful , please mark it as such.

           

          Sincerely ,

            Ubu

          • 2. Re: Issue with AddEventListener
            lkenney2 Level 1

            Hi Ubu,

             

            That doesn't really help me. You mostly just reiterated what I did. This solution isn't working. Do you have any idea why?

            • 3. Re: Issue with AddEventListener
              saisri2k2 Level 4

              Application.application.addEventListener("ContentSelector.CONTENT_SECTION", onContentSelectorComplete);
                        
              
              

              Use the above line

              • 4. Re: Issue with AddEventListener
                UbuntuPenguin Level 4

                Actually , what I wrote and what you wrote are different.  You add your event listener AFTER the event has been dispatched.  An event listener can't listen for events that were fired before it was added.

                 

                Have you tried this.

                 

                public class ContentSelector extends EventDispatcher

                {

                  // lazy constructor

                  public function ContentSelector()

                  {}

                 

                  public function setSection( section:String ):void

                  {

                     /// Switch cases galore

                     dispatchEvent( new Event("ContentSelector.CONTENT_CHANGE") );

                  }

                 

                  ....

                }

                 

                Then in your code.

                 

                private var _contentSelector:ContentSelector = new ContentSelector();

                _contentSelector.addEventListener("ContentSelector.CONTENT_CHANGE" , onContentCompleteChange );

                _contentSelector.setSection("story");

                • 5. Re: Issue with AddEventListener
                  Sebastien V. Level 3

                  UbuntuPenguin is right, I think you missed out the important part of his post. You are adding the event listener AFTER the event has been dispatched, that's why it can not be catched.