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 Level 1



      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:mx="library://ns.adobe.com/flex/mx" >
              <!-- Place non-visual elements (e.g., services, value objects) here -->
                  import mx.controls.Button;
                  [Bindable]public var test: Class;




      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;



      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);



      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?