5 Replies Latest reply on Oct 12, 2009 4:10 PM by 80sRelic

    Loading Multiple Instances Of A Module

    80sRelic

      I am creating an applciation that will control golf simulators from a central location. The application will display information panels about each simulator in a tile component. The panels are modules. The below code tries to load 6 info panels if your press the button. When you press it, only 1 panel appears. If you press it a second time, 6 more appear. Does anyone have any insight as to why this occurs?

       

      Main App:

       

      <?xml version="1.0" encoding="utf-8"?>
      <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                     xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/halo">

       

          <fx:Script>
              <![CDATA[
                 
                  import mx.core.Application;
                  import mx.managers.PopUpManager;
                  import mx.events.ListEvent;
                  import mx.events.FlexEvent;
                  import mx.controls.Alert;
                  import mx.modules.ModuleManager;
                  import mx.modules.IModuleInfo;
                  import mx.controls.ProgressBar;
                  import flash.events.TimerEvent;
                  import flash.utils.Timer;
                  import mx.events.ModuleEvent;

       

                  public var info:IModuleInfo;
                  public var mySimCount:int = 6;

       

                 
                  protected function button5_clickHandler(event:MouseEvent):void
                  {
                      testPanels();
                  }
                 
                   public function testPanels():void
                  {
                     
                      var i:int;
                      for (i = 0; i < mySimCount; i++)
                      {
                          info = ModuleManager.getModule("simPanel.swf");
                          info.addEventListener(ModuleEvent.READY, modEventHandler);          
                         
                          // Load the module into memory. Calling load() makes the
                          // IFlexModuleFactory available. You can then get an
                          // instance of the class using the factory's create()
                          // method.
                          info.load();
                         
                      }
                     
                     
                  }
                 
                  private function modEventHandler(e:ModuleEvent):void {
                      // Add an instance of the module's class to the
                      // display list.
                      myTile.addChild(info.factory.create() as DisplayObject);
                  }
         
              ]]>
             
             
          </fx:Script>

       

          <fx:Declarations>
              <!-- Place non-visual elements (e.g., services, value objects) here -->
          </fx:Declarations>
         
          <s:Button  x="562" y="20" label="Button" click="button5_clickHandler(event)"/>
          <mx:Tile x="10" y="66" width="100%" height="100%" id="myTile">
          </mx:Tile>

       

         
      </s:Application>

       

       

      --------------------

       

      Module:

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Module xmlns:fx="http://ns.adobe.com/mxml/2009"
                 xmlns:s="library://ns.adobe.com/flex/spark"
                 xmlns:mx="library://ns.adobe.com/flex/halo" layout="absolute" width="250" height="350">
          <fx:Declarations>
              <!-- Place non-visual elements (e.g., services, value objects) here -->
          </fx:Declarations>
          <s:Panel x="0" y="0" width="250" height="346" dropShadowVisible="false">
              <s:Label y="-21" text="Sim Name" left="10"/>
              <s:Label y="-21" text="Sim Status" right="6"/>
              <mx:TabNavigator x="10" y="10" width="228" height="252">
                  <s:NavigatorContent label="View Stats" width="100%" height="100%">
                      <s:Label y="10" text="Course and Hole" left="10"/>
                      <mx:DataGrid x="10" y="24" height="190" width="206">
                          <mx:columns>
                              <mx:DataGridColumn headerText="Plyr" dataField="col1"/>
                              <mx:DataGridColumn headerText="Scr" dataField="col2" resizable="false"/>
                          </mx:columns>
                      </mx:DataGrid>
                  </s:NavigatorContent>
                  <s:NavigatorContent label="View Screen" width="100%" height="100%">
                  </s:NavigatorContent>
              </mx:TabNavigator>
              <s:Label y="270" text="Time Information" x="84"/>
              <s:Label y="290" text="Alerts" height="18" x="109"/>
          </s:Panel>
      </mx:Module>

        • 1. Re: Loading Multiple Instances Of A Module
          Flex harUI Adobe Employee

          Modules get loaded asynchronously.  They are not ready until at least one frame after the load request (unless they are already loaded).

           

          So, first time through, you get the same info object each time in the loop and attach the READY listener and when your code exits the loop, sometime later the READY event fires and the one info object adds a child.

           

          Second time through, you get a READY event every time you call load() and it therefore adds a child while in the loop.

           

          Alex Harui

          Flex SDK Developer

          Adobe Systems Inc.

          Blog: http://blogs.adobe.com/aharui

          1 person found this helpful
          • 2. Re: Loading Multiple Instances Of A Module
            80sRelic Level 1

            I see. Thanks for the info. I'll see if I can work out how to handle this. When my app loads, it will make a call to a PHP class, get the details for each simulator and populate each info panel. Generally, speaking, is this doable?

            • 3. Re: Loading Multiple Instances Of A Module
              Flex harUI Adobe Employee

              Try something more like this:

               

                        public function testPanels(event:Event = null):void

                          {

                              info = ModuleManager.getModule("simPanel.swf");

                              if (!info.ready)

                              {

                                  info.addEventListener(ModuleEvent.READY, testPanels);                                            

                                  // Load the module into memory. Calling load() makes the

                                  // IFlexModuleFactory available. You can then get an

                                  // instance of the class using the factory's create()

                                  // method.

                                  info.load();

                                  return;

                              }

                              var i:int;

                              for (i = 0; i < mySimCount; i++)

                              {

                                  myTile.addChild(info.factory.create() as DisplayObject);                   

                              }

                               

                               

                          }

               

              Alex Harui

              Flex SDK Developer

              Adobe Systems Inc.

              Blog: http://blogs.adobe.com/aharui

              • 4. Re: Loading Multiple Instances Of A Module
                80sRelic Level 1

                Thanks, that works nicely.

                • 5. Re: Loading Multiple Instances Of A Module
                  80sRelic Level 1

                   

                   

                  I have another question related to this. Along with creating the correct number of modules, I need to pass a variable to the module so that it can look up the correct data from the MySQL database.  I can get the module to read the variable using getdatafromparent ok. The problem I am having is that the variable is always the same. For instance if I do this in the below code:

                   

                  mySimCount = 3;

                  var simIndex:int;

                   

                  for (i = 0; i < mySimCount; i++)

                               {

                                    myTile.addChild(info.factory.create() as DisplayObject);

                                 simIndex++;

                   

                                }

                   

                  simIndex is 3 for each module loaded where I would have expected simIndex to be 1,2 or 3 depending on the module loaded.

                   

                  Also, I noted in the docs that in info.factory.create() you can define a parameter to pass. Does this mean I can use this to pass simIndex instead of using getdatafromparen? If so, how is the parameter array accessed in the module?