« | »

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"/>

Tags: ,  

3 Comments add your own

Leave a Comment

required

required, hidden

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