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:
- Flex 2.01 Developer's Guide, Creating Modular Applications, http://livedocs.macromedia.com/flex/201/html/modular_083_1.html
- Flex 2.01 Language Reference, Class Module, http://livedocs.macromedia.com/flex/201/langref/mx/modules/Module.html
- Flex 2.01 Language Reference, Class ModuleLoader, http://livedocs.macromedia.com/flex/201/langref/mx/modules/ModuleLoader.html
- 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.
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.
For example:
ModuleManager.getModule("BarChartModule.swf").load();
hth,
matt horn
flex docs
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!
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.
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?
The code for module2.swf was not included in the view source.
I too recive the error where use tabnavigator, accordion or others controls. The problem is where unload and load the module.
thanks Bruce
Now Update the sample :
http://www.managernet.com.ar/modulos/VisorModulos....
I have tried to do it.. but debugger "freezes" when the module is being loaded...
(am i doing something wrong?)
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
just found my mistake. I forgot to add the modLoader as a child of my application.
Sorry for this!
Juergen
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.
See: http://weblogs.macromedia.com/pent/archives/2007/0...
;)
implements... that is a good one..
my version is <a href="http://flex2colombia.wordpress.com/2007/01/11/debu...">here</a>
very nice works..
thanks
thankss
See: http://weblogs.macromedia.com/pent/archives/2007/0...
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
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.
very nice works..
thanks