`
SilverRing
  • 浏览: 70819 次
社区版块
存档分类
最新评论

[Flex] two ways of loading modules

    博客分类:
  • Flex
阅读更多
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
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics