flex4.1 actionscript3
代码如下:
package modulemanager
{
import flash.utils.Dictionary;
import mx.events.ModuleEvent;
import mx.modules.IModule;
import mx.modules.IModuleInfo;
import mx.modules.Module;
import mx.modules.ModuleManager;
import spark.components.Group;
/**
* module管理类
* @author yinlm
* */
public class ModuleLoaderUtil
{
public function ModuleLoaderUtil(modulecontainer:Group = null)
{
this.modulecontainer = modulecontainer;
}
private var modulecontainer:Group;
/**
* 当前module信息接口
* */
private var currentModuleInfo:IModuleInfo;
/**
* 当前module
* */
private var currentModule:Module;
/**
* 当前module的地址
* */
private var currentModuleUrl:String = "";
/**
* 使用Dictionary对加载策略为cache的Module持有引用,
* 当第二次需要使用该Module时,直接返回缓存对象,而不再创建新的对象
*/
private var moduleCacheDict:Dictionary = new Dictionary(true);
public function handleLoadModule(moduleUrl:String):void
{
if(currentModuleUrl == moduleUrl){
return;
}
if(currentModule != null){
destroyModule();
}
if(moduleCacheDict[moduleUrl] != null) {
currentModule = moduleCacheDict[moduleUrl];
modulecontainer.addElement(currentModule);
currentModuleUrl = moduleUrl;
}else{
currentModuleInfo = ModuleManager.getModule(moduleUrl);
currentModuleUrl = moduleUrl;
currentModuleInfo.addEventListener( ModuleEvent.READY, handleModuleLoad );
currentModuleInfo.load();
}
}
private function handleModuleLoad( evt:ModuleEvent ):void
{
currentModule = currentModuleInfo.factory.create() as Module;
modulecontainer.addElement(currentModule);
}
private function destroyModule():void {
if(moduleCacheDict[currentModuleUrl] == null){
moduleCacheDict[currentModuleUrl] = currentModule;
}
modulecontainer.removeElement(currentModule);
currentModule = null;
}
}
}