PureMVC Architects Lounge

Announcements and General Discussion => Fabrication => Topic started by: Tony DeFusco on March 04, 2009, 09:42:11

Title: Need help with Flex ModuleManager and fabrication FlexModules
Post by: Tony DeFusco on March 04, 2009, 09:42:11
I'm still a newbie when it comes to PureMVC and fabrication, but I feel that I'm up to the challenge of being productive with these useful frameworks. 

What I would like to know is how to properly use Flex's ModuleManager class with classes that inherit from the fabrication FlexModule class.

The Fabrication Wiki details the basics of how to load Flex modules with the Flex ModuleManager class:

Using Module Manager for Flex Modules
Flex modules can also be loaded using the ModuleManager. The ModuleManager only loads the module swf. It does not instantiate the loaded swf. That needs to be done manually after the loading has completed by invoking create on the module's factory.

Loads the module, my_module.swf using ModuleManager.

var module:IModuleInfo = ModuleManager.getModule("my_module.swf");
module.addEventListener(ModuleEvent.READY, moduleReadyListener);

private function moduleReadyListener(event:ModuleEvent):void
  var moduleInstance:Object = event.module.factory.create();

My fabrication application essentially follows the above example.  I must be doing something wrong because the xStartupCommand command class for my module is not being called, even though my modules main class specifies the xStartupCommand class in its getStartupCommand() method.

To ensure the same functionality as if the fabrication FlexLoader class had been used, are some extra configuration steps needed for using Flex's ModuleManager class to load the FlexModules?

I was able to get something working using code to the effect of the following:

var moduleInstance:FlexModule = event.module.factory.create() as FlexModule;
moduleInstance.router = (this.fabFacade.getApplication() as FlexApplication).router;

Is this essentially the right way to go about it, or is there a better way?

Title: Re: Need help with Flex ModuleManager and fabrication FlexModules
Post by: Darshan Sawardekar on March 04, 2009, 10:53:14
Hey Tony,

I am waiting for the FlexModule's initialize event to fire to trigger the startup command. You can take a look at an example in the svn[1], simple_routing_with_module_manager. It uses ModuleManager instead of  loading with FlexModuleLoader. The important code is in the ModulesContainerMediator,

                private function moduleReadyListener(event:ModuleEvent):void {
                        var module:IModuleInfo = event.module;
                        var moduleDescriptor:ModuleDescriptor = event.module.data as ModuleDescriptor;
                        var moduleInstance:Object = event.module.factory.create();
                        moduleInstance.router = applicationRouter;
                        moduleInstance.defaultRouteAddress = applicationAddress;
                        moduleInstance.id = moduleDescriptor.getElementID();
                        moduleInstance.addEventListener(FabricatorEvent.FABRICATION_CREATED, moduleCreated);
                        moduleInstance.addEventListener(FabricatorEvent.FABRICATION_REMOVED, moduleRemoved);
                        modulesContainer.addChild(moduleInstance as DisplayObject);
                private function moduleCreated(event:FabricatorEvent):void {
                        event.target.removeEventListener(FabricatorEvent.FABRICATION_CREATED, moduleCreated);
                        trace("moduleCreated " + event.target);

The addChild makes the module's initialize event to fire. If however you need to initialize the module without adding it to the DisplayList then your approach is fine as well. Just make sure to set the router on the module before you initialize it else it will throw an error.


Title: Re: Need help with Flex ModuleManager and fabrication FlexModules
Post by: Tony DeFusco on March 05, 2009, 08:49:50
Thanks Darshan for the detailed help.  Please forgive my ignorance, but where are the properties applicationRouter and applicationAddress coming from?

This fabrication utility is a wonderful extension to multi-core PureMVC -- I'm finding that it makes the multi-core framework much easier to use.