4 Replies Latest reply on Jun 11, 2009 4:07 AM by gag_sam

    ModuleLoader event dispatcher

    gag_sam

      I am using ModuleLoader in my main application file. I am loading modules via ModuleLoader based on the user interactions. I need to dispatch an event from the main application file which should be listen by every module. So, for dispatching the event I used ModuleLoader

      i.e.

      if(  moduleLoader.child != null )

      {                                              

           moduleLoader.child.dispatchEvent( new MyEvent( MyEvent.MY_EVENT ) );

      }

      And for registering the event, I write the following code in every module:

      this.addEventListener( MyEvent.MY_EVENT, onMyEvent );

       

      For first module it is working, but for rest of the module it’s not. Well, I compared all the modules but didn’t find anything. Still, I am not able to figure out the cause. If anyone has some idea regarding the same then pls. help me.

        • 1. Re: ModuleLoader event dispatcher
          leybniz Level 4

          You coould try to use Application.application as medium for Events exchange,

          for example:

           

          within each module constructor/creationComplete handler say:

          Application.application.addEventListener( MyEvent.MY_EVENT, onMyEvent );

           

          then anywhere in the application say:

          Application.application.dispatchEvent( new MyEvent( MyEvent.MY_EVENT ) )

           

           

           

          • 2. Re: ModuleLoader event dispatcher
            gag_sam Level 1

            Thanks for the quick response. But I need to register listener event in every module as I have to write an action related to each particular module.

            Actually, I want to do some action for each module when the application resizes. So, for that to achieve the idea is to dispatch an event on the resize event of application and application does not need to take care of which module is currently loaded. It should be the responsibility of each module to handle its task on resize event.

            Using Application.application can make the system tighly-coupled.

            • 3. Re: ModuleLoader event dispatcher
              leybniz Level 4

              I see, that way you could go with OOP, extract BaseModule class out of your modules, and addEventListener once there.

               

              Better solution is to maintain an internal list of loaded modules and enum them to notify each of them each time an Event occurs, for that

              you could introduce ModuleDescriptor class which will carry the reference to the loaded module instance plus handy information

              on what kind of module it is, etc.

              • 4. Re: ModuleLoader event dispatcher
                gag_sam Level 1

                Thanks a lot for your effort. ModuleLoader.child.dispatchEvent is working. I did a silly mistake . Yesterday I made some changes in the .actionScriptProperties file and accidentally removed two module names.

                So, whatever changes I was making in the modules (adding event listeners) is not reflecting after compilation. So, silly mistake and I wasted my 5 hours on it today . Well, found it after cleaning my project. Thanks again for putting your efforts . I never went through the ModuleDescriptor class. Its really a learning for me.