Loading And Unloading Modules in Flex

Here is sample code to load & unload modules in Flex

import mx.managers.PopUpManager;
import mx.events.MenuEvent;
import mx.controls.Menu;
import mx.controls.Alert;
import mx.events.ModuleEvent;
import mx.modules.ModuleManager;
import mx.modules.Module;
import mx.modules.IModuleInfo;
import mx.core.IUIComponent;

private var moduleToBeLoaded:Module;

private var moduleInfo:IModuleInfo;
private var moduleName:String;

private var module:Module;

private function menuItemHandler(event:MenuEvent):void
//Alert.show(“Menu:\t”+event.item.@label   +”  url:\t”+event.item.@url+”  modulename:\t”+event.item.@modulename);
moduleInfo = ModuleManager.getModule(event.item.@url);

moduleInfo.addEventListener(ModuleEvent.READY, renderModule);
moduleInfo.addEventListener(ModuleEvent.ERROR, handleError);


private function renderModule(event:ModuleEvent):void


if(moduleName == “M1”)
moduleToBeLoaded = moduleInfo.factory.create() as M1; // M1 is mxml file contains <mx:Module> ….</mx:Module>

else if(moduleName == “M2”)
moduleToBeLoaded =  moduleInfo.factory.create() as M2;

else if(moduleName == “M3″)
moduleToBeLoaded =  moduleInfo.factory.create() as M3;


private function handleError(event:ModuleEvent):void


<mx:ApplicationControlBar id=”AppControlBar” paddingTop=”0″ paddingBottom=”0″ width=”100%” height=”55″
styleName=”MyApplicationControlBar” >
<mx:MenuBar id=”appMenuBar” labelField=”@label” width=”100%” height=”41″ fontWeight=”bold” fontSize=”12″
themeColor=”#E7790D” alpha=”1.0″ itemClick=”menuItemHandler(event)”>

<menuitem label=”Loading And Unloading Modules” >
<menuitem label=”Module 1″ url=”M1.swf” modulename=”M1″ />
<menuitem label=”Module 2″ url=”M1.swf” modulename=”M2″/>
<menuitem label=”Module 3″ url=”M3.swf” modulename=”M3″/>


<mx:Canvas id=”applicationModuleCanvas” width=”100%” height=”95%” cornerRadius=”0″ backgroundColor=”#FDFDFD” borderStyle=”inset”>



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s