Example Of Using Modules In Flex 2.01

Flex 2.01 includes support for modules. Modules can be used to organize your Flex code into separate files. These files are compiled into SWFs and then can be loaded into your main application SWF at runtime. This is my first attempt are using modules and I've posted an example application (right click on the example to view the source files).

My references are:

  1. Flex 2.01 Developer's Guide, Creating Modular Applications, http://livedocs.macromedia.com/flex/201/html/modular_083_1.html
  2. Flex 2.01 Language Reference, Class Module, http://livedocs.macromedia.com/flex/201/langref/mx/modules/Module.html
  3. Flex 2.01 Language Reference, Class ModuleLoader, http://livedocs.macromedia.com/flex/201/langref/mx/modules/ModuleLoader.html
  4. FlexCoders Yahoo Group, Compiling Flex Modules, http://tech.groups.yahoo.com/group/flexcoders/message/60494

My module demonstration application is based on the example in chapter 31 of the Flex 2.01 Developer's Guide (see reference 1). I am using a tab navigator to display various charts. Each chart's MXML code is stored in a separate module (right click on the example application, view source, and select LineChartModule.mxml or PieChartModule.mxml or ColumnChartModule.mxml).

Modules can be used to separate out of the main application, code that doesn't need to be loaded when the Flex application first loads in the user's browser. In my example, the charts that are on the non-visible tabs don't need to be loaded initially. When the user clicks on a tab, the SWF file containing that tab's chart is loaded.

Since not all the code is wrapped into one large SWF, using modules can reduce the initial load time. Also using modules can make your code easier to maintain and reuse. Since the code in a module is a self-contained entity, I could use the same chart module in another application. Additionally, if I change just the base application, I don't need to recompile all the other modules. If I change one of the modules, I only need to recompile that module. Using modules also supports team creation of a Flex application. According to reference 1, it is also possible to link to modules that are on different servers.

To create a module, I used File - New MXML Component and in the list of "Based On" components, I selected module. Between the opening and closing module tag, you place your code. In my example application, the modules are loaded automatically when a tab is selected. However, you can load and unload your modules manually. There is an example of how to do that in reference 1 above. Additionally, the loading and unloading of modules can generate events.

To load a module you use the ModuleLoader tag. You provide the URL attribute the path to and the name of the SWF that contains the code for the module. I did notice a slight delay the first time a tab was selected until the SWF for that module was loaded. It appears that the browser must go back to the server to download the SWF (I thought perhaps all the module SWFs would be downloaded in the background after the application loads).

Compiling a module in Flex Builder 2.01 is not self-evident. There is no "compile module" command button or menu choice. Consult reference 4 above for some ways to compile a module. What I did in my example, was to right-click on the module name in the navigator panel, select Run As Flex Application. This command will create the .swf and .html files for the module and try to load it in the browser (where nothing will display). When you upload your application, you just need to upload the SWF files for each module, in addition to the other main application files.

As I mentioned at the beginning, this is my first attempt using the new module feature. Please post any comments to other module examples and let me know if I have misunderstood something about modules.

Comments
"It appears that the browser must go back to the server to download the SWF (I thought perhaps all the module SWFs would be downloaded in the background after the application loads)."

That is correct: There will be a delay because the modules are not downloaded in teh background. They are only loaded when requested. The browser caches them, though, so if you unload a module and then load it again, there will be no delay due to downloading.
# Posted By Matthew Horn | 1/8/07 1:44 PM
Further explanation... You CAN preload modules by using the IModuleInfo's load() method in, say, an application's creationComplete event handler. This will load the module but not display it.

For example:
ModuleManager.getModule("BarChartModule.swf").load();

hth,
matt horn
flex docs
# Posted By Matthew Horn | 1/8/07 3:04 PM
More to the point, how are you supposed to get data into and out of a module? Along with "how the hell do we compile these things in Flex Builder", this seems to be the most asked question at the moment.

Much as I appreciate Adobes hard work I think I would have rather waited another week and had some *usable* documentation. We know you're busy guys but so are we - my job is to build apps to deadline for clients, not to spend hours (along with hundreds of other people) second-guessing how changes to the tools work!
# Posted By ark | 1/9/07 5:00 AM
Matthew - thanks for posting more information about how to preload modules.

Ark - I'll continue to research and experiment with modules. But right now I'm thinking of using modules only as completely self-contained entities. My main application will not be exchanging data with the modules. I envision using modules to help me organize my code, make it easier to maintian, and lessen the initial download when its appropriate. Using modules may not be correct in some cases.

Adobe has posted updated documentation (see my references) and will continue to post new examples in their Developer center and Quick Start center on http://www.adobe.com/flex. Also much smarter Flex developers than I will be posting about how to use modules and what we can do with them. Finally, hopefully, some really good Flex 2 books will come out with even better code examples and information.

Lastly, since I don't work for Adobe, you may want to submit a change request to their Live Docs (see reference 2 above) for more examples and clarification. I've submitted changes to Adobe's Live Docs in the past and was impressed by their quick response. Just click on the Comment button that is on the bottom of a Live Docs page.
# Posted By Bruce | 1/9/07 6:41 AM
Hi Bruce, you app works fine.

I a design an modular application and having the following problem:

I've built a module (called Modulo1.swf) that contains a DataGrid and a couple of controls.
Then I made another module (called Modulo2.swf) that also contains a DataGrid and an Accordion.
Then I made a project called VisorModulos (ModuleViewer), it only load and unload this modules.
The error occurs after loading and unloading twice a module. It fails with the first module and also with the second, here is the link:

http://www.managernet.com.ar/modulos/VisorModulos....

To reproduce the error you must load and unload at least twice any module. In case of the first module, after loading for the second time, try using the DataGrid.

Right click to view source.

Any idea?
# Posted By Fabián Brussa | 1/9/07 11:31 AM
It looks like to me there is some problem with the code for module2.swf. I do get an error when trying to load module 2. I don't get an errors when loading or unloading module 2 several times in a row.

The code for module2.swf was not included in the view source.
# Posted By Bruce | 1/9/07 12:51 PM
Bruce.. the code for module2.swf is veri simple, only componentes whitin Script code. Try this: loading or unloading module 1 several times and try use the DataGrid.

I too recive the error where use tabnavigator, accordion or others controls. The problem is where unload and load the module.

thanks Bruce
# Posted By Fabián Brussa | 1/9/07 1:15 PM
Hi Bruce..

Now Update the sample :

http://www.managernet.com.ar/modulos/VisorModulos....
# Posted By Fabián Brussa | 1/9/07 2:34 PM
What are the best practices to debug modules?
I have tried to do it.. but debugger "freezes" when the module is being loaded...
(am i doing something wrong?)
# Posted By Juan Carlos M | 1/9/07 8:48 PM
Hi,

have you ever tried to load a module by using the moduleLoader as instance in a Actionscript (e.g. modLoader:ModuleLoader = new ModuleLoader()). I did that and I received no runtime-error but as a result the module to be loaded becomes never visible although I get the ready event from the loader.

Any ideas?

Regards Juergen
# Posted By Juergen Werle | 1/10/07 2:23 AM
Hi folks,

just found my mistake. I forgot to add the modLoader as a child of my application.

Sorry for this!

Juergen
# Posted By Juergen Werle | 1/10/07 2:37 AM
Juan:

I'm not sure what the "best practices" are for debugging modules. I've not done that myself. You may want to post your question on the FlexCoders Yahoo group (see my reference 4 above).

Fabian: I'm not able to determine what the problem is in your code.
# Posted By Bruce | 1/10/07 9:40 AM
Peter Ent posted an excellent example and tutorial with a code download on how to use Flex modules and how to communicate with modules.

See: http://weblogs.macromedia.com/pent/archives/2007/0...
# Posted By Bruce Phillips | 1/12/07 8:04 PM
Nice tips in http://weblogs.macromedia.com/pent/archives/2007/0...
;)
implements... that is a good one..
# Posted By Juan Carlos M | 1/13/07 6:30 AM
Hanks for this, it helped me get started. I also wrote up how to debug modules easily here: http://blog.kevinhammer.net/2007/01/debugging-with...
# Posted By Kevin Hammer | 1/16/07 12:40 AM
Hey, there's a new modules chapter here: http://blogs.adobe.com/flexdoc/
# Posted By Matthew Horn | 1/16/07 9:11 AM
I have a similar (or equal :P) trick to do the same as Kevin Hammer's <a href="http://blog.kevinhammer.net/2007/01/debugging-with... ">Debugging with Flex Modules</a>

my version is <a href="http://flex2colombia.wordpress.com/2007/01/11/debu...">here</a>
# Posted By Juan Carlos M | 1/16/07 5:24 PM
hi
very nice works..
thanks
# Posted By lazer epilasyon | 3/12/07 5:52 AM
Thanks this post
# Posted By Matbaa | 3/21/07 11:34 AM
I too recive the error where use tabnavigator, accordion or others controls. The problem is where unload and load the module.
# Posted By Dekorasyon | 3/21/07 11:35 AM
Thanks.. this post
# Posted By Estetik | 3/21/07 11:36 AM
Very goog comments. thanks
# Posted By Lazer Epilasyon | 3/21/07 11:36 AM
I'm not sure what the "best practices" are for debugging modules. I've not done that myself. You may want to post your question on the FlexCoders Yahoo group (see my reference 4 above).
# Posted By Estetik | 5/18/07 7:57 PM
You CAN preload modules by using the IModuleInfo's load() method in, say, an application's creationComplete event handler. This will load the module but not display it.

See: http://weblogs.macromedia.com/pent/archives/2007/0...
# Posted By estetik | 5/18/07 8:01 PM
Thank you..
# Posted By zonenoktaog ödüllü seo yar??mas? | 5/21/07 9:51 AM
Hi Bruce,

I tried to work out the sample in my Flex Builder. But my Flex builder doesn't recognizes mx:Module as well as mx:ModuleLoader command. What could be the problem.

I am using Flex builder 2.01
# Posted By ashwath | 5/28/07 9:43 PM
ashwath, sounds like you need to upgrade to 2.0.1. This is new functionality in that release.
# Posted By Matthew Horn | 5/29/07 6:35 AM
Ashwath - As Matthew states above. Check your Flex Builder version. A new version was released in January 2007. In Flex Builder click on HELP - ABOUT Adobe FlexBuilder 2. Version must be 2.0.1. This blog entries title is screwed up (2.01). Which to make it even more confusing, 2.01 was a previous version before 2.0.1 release.

See: http://www.adobe.com/support/flex/ for the 2.0.1 download.

I've never understood why Adobe released a new version of Flex and Flex Builder but named the version 2.0.1 instead of something like 2.5.
# Posted By Bruce | 5/29/07 10:42 AM
very nice module.Thank you
# Posted By tuzcuo?lu | 6/5/07 6:32 AM
sounds like you need to upgrade to 2.0.1. Thanks.
# Posted By galatasaray resimleri | 6/5/07 6:33 AM
This is nice.Congratulations and thank you.
# Posted By arama motoru optimizasyonu | 6/5/07 6:34 AM
Thank you bruce.
# Posted By endustri muhendisi | 6/5/07 6:41 AM
This is good.Thanx
# Posted By tuzcuoglu nakliye | 6/7/07 2:43 PM
thanks sir, its been very usefull for me but i am also searching for <mx:Module> application
# Posted By shashank | 6/19/07 11:17 PM
Nice module.Thank you
# Posted By iddaa tahminleri | 6/23/07 1:34 PM
thank you very much
# Posted By Mehmet Poyraz | 7/1/07 7:42 AM
hi
very nice works..
thanks
# Posted By ceviri | 7/1/07 3:04 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.1.004.