Reputation: 7294
I have main application which contains ModuleManager
. Different modules are loaded by this application. Both main application and loaded modules use my custom RSL. I need to get Class object in my RSL, which is defined in one of the modules. I'm trying to use getDefinitionByName
function, but since my class is not defined in RSL, I get an exception (though module with needed class is loaded). Is it possible to make module classes visible to RSL code and to get the instance of it at runtime without changing project structure? Thanks
Upvotes: 2
Views: 920
Reputation: 4708
When you load a new module, specify an Application Domain. As the document says:
"The ApplicationDomain class is a container for discrete groups of class definitions."
You specify an Application Domain as part of the Loader Context when you load the SWF.
Once you have a reference to the Application Domain that the module is loaded in to, you can call the getDefinition()
method of the Application Domain to get the definition, in much the same way as getDefinitionByName()
Also see the following document, "Working with Application Domains," for a great description of exactly how they work.
http://help.adobe.com/en_US/as3/dev/WSd75bf4610ec9e22f43855da312214da1d8f-8000.html
And here's a copy paste of the example, just incase is wanders off:
package
{
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.*;
import flash.net.URLRequest;
import flash.system.ApplicationDomain;
import flash.system.LoaderContext;
public class ApplicationDomainExample extends Sprite
{
private var ldr:Loader;
public function ApplicationDomainExample()
{
ldr = new Loader();
var req:URLRequest = new URLRequest("Greeter.swf");
var ldrContext:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);
ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
ldr.load(req, ldrContext);
}
private function completeHandler(event:Event):void
{
var myGreeter:Class = ApplicationDomain.currentDomain.getDefinition("Greeter") as Class;
var myGreeter:Greeter = Greeter(event.target.content);
var message:String = myGreeter.welcome("Tommy");
trace(message); // Hello, Tommy
}
}
}
Upvotes: 1
Reputation: 6961
You can inject the definition of the Class if you expose a property on the Module (or its Interface) of type Class, similar to how Class definitions are injected into buttons to make the icons.
So, your Module might have code like this:
protected var _classToMake:Class;
public function get classToMake():Class {
return _classToMAke;
}
public function set classToMake(value:Class):void {
if (value != _classTomake) {
if (value != null) {
//test to make sure we're making the right thing
var testClass:SomeType = new value() as SomeType;
if (testClass != null) {
_classToMake = value
} else {
trace('classToMake must be a definition that makes a class of SomeType');
}
}
}
}
Upvotes: 1