3 Replies Latest reply on Sep 1, 2010 3:25 PM by Flex harUI

    flex 4 modules - how can i properly add a button from a module ?

    kfirufk

      Hello.

       


      I created a flex 4 module that contains a test variable type Class that contains a button element from a flash file:

       

      <?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/mx" >
          <fx:Declarations>
              <!-- Place non-visual elements (e.g., services, value objects) here -->
          </fx:Declarations>
          <fx:Script>
              <![CDATA[
                  import mx.controls.Button;
                  [Embed(source="file.swf",symbol="the_button")]
                  [Bindable]public var test: Class;

       

              ]]>
          </fx:Script>
      </mx:Module>

       

      In my main application's init function i load the flex module:

       

                  import mx.events.ModuleEvent;
                  import mx.modules.IModuleInfo;
                  import mx.modules.ModuleLoader;
                  import mx.modules.ModuleManager;
                 
                  public var assetModule:IModuleInfo;
                  public var sm:Object;
                 
                  private var ml:ModuleLoader;
                 
                  private var uri:String='http://url/flexmodule.swf';

       

                  private function init():void {
                      ml = new ModuleLoader();
                      var urlRequest:URLRequest = new URLRequest(uri);
                      var loader:URLLoader = new URLLoader();
                      loader.dataFormat = URLLoaderDataFormat.BINARY;
                      loader.addEventListener(Event.COMPLETE,itemCompleteHandler);
                      loader.load(urlRequest);

                }

       

      and this is the itemCompleteHandler() function:

       

       

                  private function itemCompleteHandler(e:Event):void {
                      var myModuleBytes:ByteArray = ByteArray(URLLoader(e.target).data);
                      ml.addEventListener(ModuleEvent.READY, getModuleInstance);
                      ml.addEventListener(ModuleEvent.ERROR,handleError);
                      ml.loadModule(uri,myModuleBytes);

                  }

       

      and the getModuleInstance function:

       

                  public function getModuleInstance(e:ModuleEvent):void {
                      Alert.show('started module instance');
                      sm = ml.child as Object;

       

                      var myElement:Class = sm.test;
                 }

       

       

      two scenarios:

       

      1. the symbol that i loaded is an image. then i can just create an Image object and to point the source attribute to myElement class and it will work.

      2. the symbol is a button, i cannot seem to find any way to cast the class properly to a button class.

       

      any ideas how can i use the Class element that i received from the flex module as a button element?

       

       

      thanks