Managing Flex Modules
May 13th, 2007When working with modules the “edit-compile-run” cycle can get in your way: Compiling the main application and then the separate modules, managing multiple Flex Builder/Ant projects, etc. For developing, it would be best to have just one big project. Here is how we do it…
- Keep each of your modules in a separate directory, declaring it as mx:Module (in this example the module is ImageManager)
- In your main application beneath your src directory, have the following two source directories:
- mod-access/extern
The ImageManagerAccess.mxl here is just a wrapper for the mx:ModuleLoader:<mx:ModuleLoader xmlns:mx=“http://www.adobe.com/2006/mxml” width=“100%” height=“100%” creationComplete=“url=’ImageManager.swf’; loadModule()”/> - mod-access/embed
The ImageManagerAccess.xml includes the Module ImageManager itself:<local:ModAccessEmbed xmlns:imageManager=“imageManager.*” xmlns:local=“*” width=“100%” height=“100%”>
<imageManager:imageManager/>
</local:ModAccessEmbed>The class ModAccessEmbed mimics the behavior of the mx:ModuleLoader class. The code shows an excerpt
public class ModAccessEmbed extends Canvas
{
public function loadModule() : void
{
…
}
public var url:String;
… // additional methods from mx:ModuleLoader as needed
}
- mod-access/extern
- Wherever you want the module to appear, don’t use a mx:ModuleLoader, but the corresponding ImageManagerAccess
- If you want your Flex project to contain all modules code in the main .swf, put mod-access/embed into your projects source path. If you want to build it separately, put mod-access/extern into your source path and remove the mod-access/embed line.
This way, you can easily switch (by just swapping the source pathes) between building one swf that contains the complete modules and compiling main application and modules separately. Or use Flex Builder to develop the integrated version and an Ant build.xml for compiling the final application.swf as well as the swf for the modules.
Useful links for working with modules/RSLs:
- Peter Ent gives an overview about how to use modules
- Farata Systems has an article about various Flex tricks
- James Ward shares his insights into compiling RSLs in order to to shrink them
- This one describes problems with sharing stuff between the main application and the modules