Loading and Unloading Modules Based on Menu , MenuItem Selection

Here is sample code to load & unload modules in Flex based on Menu , MenuItem Selection

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;

/*  Method used to get which menu item selected */
private function menuItemHandler(event:MenuEvent):void

/*  Method determines modules to be loaded from given URL */
private function loadModule(moduleURL:String):void{
moduleInfo = ModuleManager.getModule(moduleURL);
moduleInfo.addEventListener(ModuleEvent.READY, renderModule);
moduleInfo.addEventListener(ModuleEvent.ERROR, renderModuleError);

/* Loads module into Parent Container */
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


/* Error Display Wile loading Modules  */
private function renderModuleError(event:ModuleEvent):void
if(event.errorText==”Error #2035″){
Alert.show(“Please Provide Module URL in MenuItem URl Property”,”Module Not Found”);

/*  Method used to get which menu is selected */
private function menuHandler(event:MouseEvent):void
var selectedMenu:MenuBarItem =MenuBarItem(event.target)
if (XMLList(selectedMenu.data).*.length()==0)
var moduleURL:String=MenuBarItem(event.target).data.@url;
// logout code snippet
Alert.show(“Please Provide Code Snippet to Logout / Provide URL to Menu …”,”Logout”);
}—————<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)” click=”menuHandler(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