ModuleLoader ApplicationDomain
27th May 2009
By default, the module loader loads modules in a new application domain. This means the classes in the module are not accessible to the main application, which can cause problems particularly when using reflection (I stumbled into this when using SmartyPants with modules).
Fortunately, the ModuleLoader component has a property for setting the application domain of the module it loads – the unsurprisingly named applicationDomain property. Setting this to ApplicationDomain.currentContext before loading the module brings the module into the same domain as the main application, and all is well.
Except, if you want to load the module immediately you may be tempted to do something like this.
<mx:ModuleLoader url="module.swf"
applicationDomain="{ApplicationDomain.currentDomain}"/>
Unfortunately, this won’t work because the module loader will start loading the module before the application domain has been set – setting properties to static values (as for the url in this code) happens before setting properties to bound values (as for the applicationDomain in this code) so the url is set first and triggers the load before the applicationDomain is set.
To solve this, you need either to load the module later or set the applicationDomain earlier.
To load the module later:
<mx:ModuleLoader id="modLoader"
applicationDomain="{ApplicationDomain.currentDomain}"
creationComplete="modLoader.loadModule('module.swf')"/>
To set the applicationDomain sooner:
<mx:ModuleLoader id="modLoader"
url="module.swf"
preinitialize="modLoader.applicationDomain=
ApplicationDomain.currentDomain"/>

3 Comments add your own
Ah, thank you, sir! That is a very helpful piece of information. You’ve saved me a lot of work.
Nils | 29th July 2009 at 23:25
thanks. it really help me a lot.
zhenquan | 23rd August 2009 at 11:55
Hi, is there anyway to check that the correct domain has been assigned to the module.
Try sth stupid like equality but it doesn’t work.
Any idea.
phish | 19th October 2009 at 09:59
Leave a Comment comment policy
XHTML: you can use these tags - <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>Subscribe to the comments via RSS Feed