I know I am not the first to encounter this, but I thought it worth a mention anyway. I ran in to an issue loading a module from with in a module using flex. The module would load, but the ModuleEvent.READY would not fire. The instance of the module was ready and setup was true. WTF!!! why! After searching the net I came across this bug https://bugs.adobe.com/jira/browse/SDK-14669. It said a workaround is to load all the modules sequentially. Now I know this sucks because 1 by 1 is not exactly a model of efficiency. Anyhoo…I threw the following class together and thought it would help:
/**
* this class is to get around this bug
* https://bugs.adobe.com/jira/browse/SDK-14669
* when we move to gumbo (flex 4) the bug will be fixed
* */
package com.timothyhuertas.utils
{
import flash.events.EventDispatcher;
import flash.events.IEventDispatcher;
import flash.system.ApplicationDomain;
import flash.system.SecurityDomain;
import flash.utils.ByteArray;
import mx.events.ModuleEvent;
import mx.modules.IModuleInfo;
public class SequentialModuleLoader extends EventDispatcher
{
private static var _instance:SequentialModuleLoader;
private var _pool:Array;
private var _current:IModuleInfo;
public function SequentialModuleLoader(value:SingletonEnforcer)
{
_pool = new Array();
}
public static function get instance() : SequentialModuleLoader
{
if(!_instance)
{
_instance = new SequentialModuleLoader(new SingletonEnforcer());
}
return _instance;
}
public function load(info:IModuleInfo, applicationDomain:ApplicationDomain=null, securityDomain:SecurityDomain=null, bytes:ByteArray=null) : void
{
var queued:Object = new Object;
queued.info = info;
queued.applicationDomain = applicationDomain;
queued.securityDomain = securityDomain;
queued.bytes = bytes;
_pool.push(queued);
processPool();
}
protected function processPool() : void
{
if(_pool.length && _current==null)
{
var queued:Object = _pool[0];
_pool.splice(0,1);
_current = queued.info as IModuleInfo;
_current.addEventListener(ModuleEvent.READY, handleModuleResponse, false,0,true);
_current.addEventListener(ModuleEvent.ERROR, handleModuleResponse, false,0,true);
_current.load(queued.applicationDomain, queued.securityDomain, queued.bytes);
}
}
protected function handleModuleResponse(e:ModuleEvent) : void
{
IEventDispatcher(e.target).removeEventListener(e.type, handleModuleResponse);
_current = null;
processPool();
}
}
}
class SingletonEnforcer{}
An example of its usage is as follows:
var m:IModuleInfo = ModuleManager.getModule("Mod1.swf");
m.addEventListener(ModuleEvent.READY, modLoaded);
SequentialModuleLoader.instance.load(m,ApplicationDomain.currentDomain);
protected function modLoaded(e:ModuleEvent):void
{
IEventDispatcher(e.target).removeEventListener(e.type, modLoaded);
IModuleInfo(e.target).factory.create();
}
I hope this helps