Making controls work when using Modules in Flex

In my last post I explained roughly what modules are and how you can break your application into smaller chunks allowing for a faster download and better user experience.  In this post I want to pass on a solution to a problem that had me outfoxed for a day or so and which will benefit you greatly when using Modules.

First a quick description of the problem.

I have two Modules one acts as a front page and this has a grid a textbox and a calendar in side a panel.; my second Module has a couple of combo’s and another grid on it.  Each module has code specific to the operation of the Module although each has a common set of classes because they both use a grid.

The problem here is that once the first module has loaded, all the relevant classes are loaded into the application but they are only visible by that module.  When the second module loads the framework realises that the classes are already loaded and doesn’t load them again.  The second module can’t see the classes though because they were loaded by the first module.  It does take a bit to get your head round this and I recomend you read Alex Harui’s presentation on Modules, it’s the March 7 2007 entry.  This will give you a lot more in depth info on what I’m trying to convey.

So, I have a nicely modularised app but I get an error everytime I switch between modules and try to use the controls.  The answer here is to inform Flex that the modules classes should be loaded into the application domain, this makes them visible to all modules.
It took a bit more digging around and the assistance of a Cardboard Programmer to get there but I eventually found that ModuleLoader has a property allowing you to set the application domain.  So taking my sample from the previous post, changing the ModuleLoader by adding in the new applicationDomain property so it now looks like the code below solves the issue.

<mx :ModuleLoader id="module1" url="modules/homePage.swf" applicationDomain="{ApplicationDomain.currentDomain}"/>
<mx :ModuleLoader id="module2" url="modules/testReports.swf" applicationDomain="{ApplicationDomain.currentDomain}"/>

This also solved an issue with the showEffect not working on my canvas.  That’s a primer for my next post ;0)

Tags: , ,

Leave a Reply