5 Replies Latest reply on Oct 10, 2011 10:54 AM by pauland

    HOW DO YOU SEND AN EVENT "OUT" TO A COMPONENT?

    William Spence

      I am writing an AIR app that has many components.  The main application has a "start" button to begin a new project, and when you exit a project, the application returns to the start page.  Even though you exit back to the start page, the major MXML components used in the application do not get destroyed because there is no way to destroy them so they just sit there idle in the background and not visible in the "start" state.  The problem is that when a user clicks the "start" button again to start a new project, the MXML components appear in the new project in the old state that they were in because there is no obvious way to reinitialize them.  If there was a way to destroy the components, then when they were recreated in a new project I could initialize them with a creationComplete handler.  But since you can't literally destroy an MXML component, I need a way for the components to be able to listen to the main program for a click event on the "start" button, and then each component initializes itself when it hears the event.  So instead of components passing an event up to the main application, I need the main application to send an event down to the components.  Is there a way to send an event from the main application out to the various components?  (or is there a way to destroy an MXML component so that you can recreate it later that I am unaware of?)

        • 1. Re: HOW DO YOU SEND AN EVENT "OUT" TO A COMPONENT?
          pauland Level 4

          Basically you want your components to listen for the message being broadcast. If you have an MVC pattern, your components can listen for changes in your controller or the model to indicate that they should change state. Have a single object to which components can listen and use that object to dispatch the event. Then all your components will get the event.

           

          Getting access to the event could either be via a singleton, global variable, static member, etc - as you wish.

          • 2. Re: HOW DO YOU SEND AN EVENT "OUT" TO A COMPONENT?
            William Spence Level 1

            Thanks for the insight pauland, you are a great contributor on this site.  After a little research, I think I get what you are saying.  Are you suggesting that I create a global object, place a listener in each of the components for a PropertyChangeEvent, and then change the value of the global object each time the "start" button is clicked?  Thanks.

            • 3. Re: HOW DO YOU SEND AN EVENT "OUT" TO A COMPONENT?
              pauland Level 4

              Yes, that will do the job, though it's better to use a custom event and you don't need a property.

               

              I quickly made a tiny example in Flash using a static variable as a central dispatch hub..

               

              Create a class:

              package  {

                  import flash.events.EventDispatcher;

                 

                  public class EventHub extends EventDispatcher {

               

                      public static var dispatcher:EventHub = new EventHub();

                      public function EventHub() {

                          // constructor code

                      }

                  }

              }

               

               

              // in the component add this

              EventHub.dispatcher.addEventListener("INIT_COMPONENT", _onInitComponent);

               

              // the handler for the event inside your component

              private function _onInitComponent(e:Event):void{

                  trace("INIT_COMPONENT received");

              }

               

              // wherever you need to trigger init, add this

              EventHub.dispatcher.dispatchEvent(new Event("INIT_COMPONENT"));

               

              Paul

              • 4. Re: HOW DO YOU SEND AN EVENT "OUT" TO A COMPONENT?
                William Spence Level 1

                Wow Paul, you really went above and beyond.  Thanks so much for the post, I will get to work on it right away.

                 

                You have mention MVC in a couple of posts to me in this thread and others that you have responded to.  Do you use a particular MVC architecture like Cairngorm, Mate, PureMVC, or Robotlegs?  Thanks.

                • 5. Re: HOW DO YOU SEND AN EVENT "OUT" TO A COMPONENT?
                  pauland Level 4

                  I tend to mix my own - depends on the project complexity. Simple is better than complicated.

                   

                  Another thing I try and do is always use constants for the event names - that way you have a list of events to hand and there is no chance of a spelling mistake making for a difficullt to find bug.