10 Replies Latest reply on Nov 16, 2011 5:58 AM by LeeWazHere

    Flex 3 Module Communication

    ashast1

      Hi:

      I am new in flex development. It'd be great if anyone can help me on this.

      I am try to communication between parent application and modules. Here is my code :

       

      <?xml version="1.0"?>
      <!-- modules/mxmlmodules/SimpleMXMLApp.mxml -->
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="initApp()">
        <mx:Script>
          <![CDATA[
              import mx.modules.IModuleInfo;
              import mx.modules.ModuleManager;
              import mx.controls.Alert;
      
              public var moduleInfo:IModuleInfo;
              public var sm:SimpleModule;
              
              [Bindable]
              public var answer:String;
      
              public function initApp():void {        
                  
                  moduleInfo = ModuleManager.getModule("SimpleModule.swf");    
                  moduleInfo.addEventListener("ready", getModuleInstance);
                  moduleInfo.load();
              }
      
              public function getModuleInstance(e:Event):void {
                 var moduleInfo:IModuleInfo = e.currentTarget as IModuleInfo;
                  tile.addChild( moduleInfo.factory.create () as SimpleModule);
                  sm = moduleInfo.factory.create() as SimpleModule;
              }            
              
              public function DisplayTitle():void {
             answer = sm.test();
             Alert.show(answer);
              }
          ]]>
        </mx:Script>
      
        <mx:Canvas width="1002" height="780" horizontalCenter="0" backgroundColor="#3E3E3E" top="12">
        <mx:Tile id="tile" width="766" height="616" top="72" left="5">
          <mx:Button label="Result" click="DisplayTitle()" id="btnBack" top="100" left="100" />
        </mx:Tile>
        </mx:Canvas>
      </mx:Application>
      

      Module mxml:

      <?xml version="1.0"?>
      <!-- modules/mxmlmodules/SimpleModule.mxml -->
      <mx:Module xmlns:mx="http://www.adobe.com/2006/mxml">
          <mx:Script>
              <![CDATA[
                  public var temp:String;
                  
                  public function test():String{
                      temp = txtChapterName.text;
                      return temp ;
                  }
              ]]>
          </mx:Script>
          <mx:Form width="307" height="400">
              <mx:FormItem required="true" width="250">
              <mx:TextInput width="238" height="33" id="txtChapterName"  styleName="textChapter" text="hello"/>
              </mx:FormItem>
              
              
          </mx:Form>    
      </mx:Module>
      

      when I Click on Result Button, it gives error -
      Cannot access a property or method of a null object reference.

      Please help me on this

      Thanks in advance.

        • 1. Re: Flex 3 Module Communication
          TanweerHq

          Hi,

           

          I saw ur code..

          Place the module in a Folder

          SampleModuleFolder - SampleModule

           

          This error generally occurs when the Module is not yet loaded and the method in the module has been called..

          Also..There is no need of using the IModuleFactory Instance.Follow the steps above

           

          1. In UI drag the ModuleLoader Component(say mymodule).

          2. In code..

          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="initApp()" xmlns:ns = "ModuleFolder.*">

           

          Import sampleFolder.*;

           

          Remember...

          It is always a best practice to know whether the Module is loaded..

          Use a public boolean variable in the parent(say modLoaded).

          in the initialize event of the module set this.parentApplication.bolloaded = true;

          now in the parent

           

          3. In InitApp() method write

          mymodule.loadmodule('ModuleName');

          if(bolloaded)

          {

               mymodule.method in the module..

          }

           

           

           

           

          try it and let me know

           

           

          Message was edited by: TanweerHq

          • 2. Re: Flex 3 Module Communication
            rgadiparthi Level 2

            Module Ready event not dispatched to run the function getModuleInstance()

             

            There is no ready event, use ModuleEvent.Ready in your eventlistner and try.

             

            Regards,

            Ranga G

             


            • 3. Re: Flex 3 Module Communication
              ashast1 Level 1

              TanweerHq:

               

              Hi Tanweer:

               

              Thanks for putting effort on this. Actually I have to load and unload module in my application in runtime, so I have use Module Manager to do this. rather than i have tried your code, but can't solve the problem.It'd be great if you can help me in providing a dynmic module loader solution for this.

               

              rgadiparthi :

               

              I have use the "ready" event, and its working fine and call the getModuleInstance Function.

              moduleInfo.addEventListener("ready", getModuleInstance);

              Also, I have tried your syntex, but doesn't solve the problem.

               

              Thanks both of you for your efforts,

               

              It'd be great if anyone can provide solution for this.

               

              Thanks,

              • 4. Re: Flex 3 Module Communication
                Subeesh Arakkan Level 4

                Hi,

                 

                Modules are used to reduce the main application size. So if you create an instance of your module class in the main application, it is going to add up the application size.  You could use moduleloader to load the modules.Create a custom interface and make your module implement that interface, and you can access the methods by casting the loaded module to that interface.

                 

                Check these livedocs pages

                 

                http://livedocs.adobe.com/flex/3/html/help.html?content=modular_6.html

                 

                http://livedocs.adobe.com/flex/3/html/help.html?content=modular_7.html#188322

                 

                Subeesh

                1 person found this helpful
                • 5. Re: Flex 3 Module Communication
                  ashast1 Level 1

                  Hi:

                   

                  Yup, I have already seen these documents and implement also, the function in module is passing any variable to the main application but if I want to pass any control value, it gives error.

                   

                  And, I am using these modules in a large application, where I want to passing data between modules and load, unload them in runtime, I have to change my all application for this.

                   

                  anyways, I am try to do it at my end, if anyone has some other idea kindly let me know.

                   

                  Thanks,

                  • 6. Re: Flex 3 Module Communication
                    Subeesh Arakkan Level 4

                    To pass values into modules, use methods in the custom interface created. To pass value from modules to the main application, dispatch custom events

                    • 7. Re: Flex 3 Module Communication
                      ashast1 Level 1

                      Hi:

                       

                      Thanks for putting efforts in this question, Actually, I am new in flex development, It'd be great if you can provide some small example for this or you can modify my code to do this.

                       

                      Its really helpful for me.

                       

                      Thanks,

                      • 8. Re: Flex 3 Module Communication
                        Subeesh Arakkan Level 4

                        Here is a link to great documentation on modules from the Flex Doc Team.

                        http://blogs.adobe.com/flexdoc/modules/

                        It also contains a link to download all the examples used in the doc as zip file

                        • 9. Re: Flex 3 Module Communication
                          fnicollet

                          hey,

                          you get null references because your objects ain't instanciated yet. They will only be instanciated once the module is added to the display list. Simple do this: tile.addChild(sm); after sm=moduleInfo.factory.create() as SimpleModule; and your problem will be solved

                           

                          Fabien  

                          http://www.flex-tutorial.fr

                          • 10. Re: Flex 3 Module Communication
                            LeeWazHere

                            @ashast1

                            im a newbie as well but basically what fnicollet and others are saying is this sorry this is an age old response but i just found this post anyway

                             

                            public function initApp():void {       
                                       
                                        moduleInfo = ModuleManager.getModule(SimpleModule.swf);   
                                        moduleInfo.addEventListener(ready, getModuleInstance);
                                        moduleInfo.load();
                                    }

                             

                            change to

                             

                            public function initApp():void {       
                                       
                                        moduleInfo = ModuleManager.getModule("SimpleModule.swf");   
                                        moduleInfo.addEventListener(
                            ModuleEvent.READY, getModuleInstance);
                                        moduleInfo.load();
                                    }

                            and

                             

                               public function getModuleInstance(e:Event):void {
                                       var moduleInfo:IModuleInfo = e.currentTarget as IModuleInfo;
                                        tile.addChild( moduleInfo.factory.create () as SimpleModule);
                                        sm = moduleInfo.factory.create() as SimpleModule;
                                    } 

                             

                            change to

                             

                               public function getModuleInstance(e:Event):void {
                                       var moduleInfo:IModuleInfo = e.currentTarget as IModuleInfo;
                                       
                            sm = moduleInfo.factory.create() as SimpleModule;

                                        tile.addChild(sm);
                                    } 

                            these fixes would definately solve ur problem