0 Replies Latest reply on Jan 5, 2007 3:39 AM by deesri

    Flex Modular applications

    deesri
      HI,
      I am using latest Flex builder and working on flex modular applications.
      In that , I have created and loaded modules - but when i tried to dispatch event from module to appln, the appln though has a listener for the custom event dispatched by the module, is not listeneing to it. I have posted both the main appln and the module code below.

      have tried adding event listener after the modules are loaded using the ready event - u can check in the code.
      also, i have tried adding listener after the progressevent is over with bytesloaded = bytes total. but no change.

      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml"
      width="900" height="800" verticalScrollPolicy="on" horizontalScrollPolicy="on" xmlns:local="*">
      <mx:Script>

      <![CDATA[
      [Bindable]
      public var expenses:ArrayCollection= new ArrayCollection([
      {Month:"Jan", Profit:2000, Expenses:1500},

      ]);

      [Bindable]
      public var progBar:String = "";
      [Bindable]
      public var progMessage:String = "";
      private var ldr:Loader;
      public var loadModule:String="";

      private function progressEventHandler(e:ProgressEvent):void {
      progBar += "*";
      progMessage =
      "Module " +
      Math.round((e.bytesLoaded/e.bytesTotal) * 100) +
      "% loaded";

      //adding event listener only after bytes are fully loaded tried this too
      if(e.bytesLoaded == e.bytesTotal) {
      ModuleManager.getModule("BookBank.swf").addEventListener("addToCart",getData);
      ModuleManager.getModule("Modulecircle.swf").addEventListener("addToCart",getData);


      }
      }

      //This method is called on ready of the module on loading
      private function addListener(me:ModuleEvent):void {
      Alert.show("addListenermethod loaded "+me.bytesLoaded +"total "+ me.bytesTotal )
      ModuleManager.getModule("BookBank.swf").addEventListener("addToCart",getData);
      ModuleManager.getModule("Modulecircle.swf").addEventListener("addToCart",getData);

      }

      private function errorHandler(e:ModuleEvent):void {
      Alert.show("There was an error loading the module." +
      " Please contact the Help Desk.");

      trace(e.errorText);
      }

      public function createModule(m:ModuleLoader, s:String,s1:String):void {
      // if (!m.url) {
      m.url = s;
      ldr = new Loader();
      loadModule=s1;
      var req:URLRequest = new URLRequest(s);
      var ldrContext:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);
      ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
      ldr.load(req, ldrContext);

      //return;
      //}

      m.loadModule();

      }

      private function completeHandler(event:Event):void
      {
      var obj:Object=Object(ApplicationDomain.currentDomain.getDefinition("Module1"));
      var moduleFactory:IFlexModuleFactory = IFlexModuleFactory(event.target.content);
      //create an instance of the module
      if(loadModule == "Module1"){
      var mod:ColumnChartModule = Module1(moduleFactory.create());

      Alert.show("MESSAGE "+mod.expenses.getItemAt(0).Month + " method "+mod.getMessage() );
      }
      }


      public function getData(event:Event):void {
      Alert.show("getData called");
      }

      public function removeModule(m:ModuleLoader):void {
      m.unloadModule();

      }
      ]]>
      </mx:Script>

      <mx:Panel title="Modular Application Demo"
      height="100%"
      width="100%"
      >

      <mx:HBox>
      <mx:Label id="b2" text="{progMessage}"/>
      <mx:Label id="b1" text="{progBar}"/>
      </mx:HBox>

      <mx:TabNavigator id="tn"
      width="100%"
      height="100%"
      creationPolicy="auto"
      >

      <mx:HBox id="vb4" label="Book Bank">
      <mx:Button
      label="Load"
      click="createModule(BookBankLoader, l4.text,'BookBank')"
      />
      <mx:Button
      label="Unload"
      click="removeModule(BookBankLoader)"
      />
      <mx:Label id="l4" text="BookBank.swf"/>
      <mx:ModuleLoader id="BookBankLoader" ready="addListener(event)"
      progress="progressEventHandler(event)" />
      </mx:HBox>

      <mx:HBox id="vb5" label="ASModule">
      <mx:Button
      label="Load"
      click="createModule(ASModuleLoader, l5.text,'Modulecircle')"
      />
      <mx:Button
      label="Unload"
      click="removeModule(ASModuleLoader)"
      />
      <mx:Label id="l5" text="Modulecircle.swf"/>
      <mx:ModuleLoader id="ASModuleLoader" ready="addListener(event)"
      progress="progressEventHandler(event)"/>
      </mx:HBox>
      </mx:TabNavigator>
      </mx:Panel>
      </mx:Application>

      I am posting the module code in the next message