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
{
moduleName=event.item.@modulename;
loadModule(event.item.@url);
}

/*  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);
moduleInfo.load();
}

/* Loads module into Parent Container */
private function renderModule(event:ModuleEvent):void
{
applicationModuleCanvas.removeAllChildren();

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

applicationModuleCanvas.addChild(moduleToBeLoaded);
}

/* 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”);
}else{
Alert.show(event.errorText,”Error”);
}
}

/*  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;
if(moduleURL.length!=0){
moduleName=MenuBarItem(event.target).data.@modulename;
loadModule(moduleURL);
}else{
// 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)”>
<mx:XMLList>

<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″/>
</menuitem>

</mx:XMLList>
</mx:MenuBar>
</mx:ApplicationControlBar>

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

</mx:Canvas>

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s