[Flex] two ways of loading modules

There're two ways of loading and unloading modules:

ModuleLoader - higher level API
ModuleManager - lower level API

1. Using ModuleLoader

1) define a ModuleLoader


<mx:ModuleLoader id="moduleLoader"/>


2) when the button of loading the module is clicked


moduleLoader.url = "SimpleModule.swf";
moduleLoader.loadModule();


3) add module load ready listener


moduleLoader.addEventListener( ModuleEvent.READY, moduleReadyHandler);


4) define the listener method


private function moduleReadyHandler( event: ModuleEvent ): void {
trace("SimpleModule is loaded");
}


5) to unload a module


moduleLoader.unloadModule();



2. Using ModuleManager

1) load the module and specify the ready event listener


info = ModuleManager.getModule("SimpleModule.swf");
info.addEventListener(ModuleEvent.READY, moduleReadyHandler);
info.load();


2) unload the module by using the same info object


info.addEventListener(ModuleEvent.UNLOAD, moduleUnloadHandler);
info.unload();



No matter what method is used, we need to make sure when the loaded Module is safe for communication. Here is how to do it: by add a creationComplete listener in moduleReadyHandler.


moduleLoader.child.addEventListener(FlexEvent.CREATION_COMPLETE,
moduleCreationCompleteHandler);


inside moduleCreationCompleteHandler:


protected function moduleCreationCompleteHandler(event:FlexEvent):void
{
moduleLoader.child.removeEventListener(FlexEvent.CREATION_COMPLETE,
moduleCreationCompleteHandler);

// further processings...
}



Someone might encounter the memory leak issue when trying to unload a module. This is a nice article to look into:

http://blogs.adobe.com/aharui/2009/08/what_we_know_about_unloading_m.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值