Pages

April 30, 2010

Loading classes to main application domain from a child SWF

Hi guys,
As part of loading applications in the form of SWF using SWFLoader as a child applications we sometimes encounter a scenario where we need to load classes to the main application domain , in order for all the SWFs to use the same class definitions between them. The thing is, we don't our "shell" application to know all the classes of it's potential children, so we thrive on getting the child application to load their class to the main application domain.
Possible? Well, yeah :).
Here is a code snippet to show you how. The idea is to have a SWF that has the classes declared on it, and load it from the child application, when defining the application domain of the loader context to be the main application domain. How do we get the reference to the main application domain? Easily:

var loader:SWFLoader new SWFLoader();
var context:LoaderContext = new LoaderContext();
context.applicationDomain = systemManager.topLevelSystemManager.loaderInfo.applicationDomain;
loader.loaderContext = context;
loader.source = "your-classes-swf-path";

The code is pretty basic to understad (but let me know if something isn't clear).
Enjoy your weekend.

No comments: