Cairngorm Modularity…
December 7, 2007
Using modules in Flex isn’t all that difficult to understand. But as I have read on many a blog, using Cairngorm with modules is giving people fits. Well, not me…
You use modules so that you can load a portion of your application (functional or visual) and unload it independent of the main application. You do this (using Flex Builder) by extending mx.modules.Module either in a different project or the same, making sure your component is registered as an application in the properties. This Module Component then can be loaded into your main application (shell app) by use of the ModuleLoader class. Simply set the attribute ‘url’ on the ModuleLoader (which is a DisplayObject) and YourModuleLoaderID.loadModule() and YourModuleLoaderID.unloadModule(). If the module is a visual component it will be loaded wherever you place it. Likewise you can place a non visual module anywhere and reference it by id. This is where the difficulty starts.
Now that you have a loaded module, unless it’s a trivial thing, you are going to want to interact with it. With Cairngorm this is very interesting. All sorts of things can happen when you get an application running with the FrontController and ServiceLocator patterns then you just up and load an entirely autonomous Cairngorm implementation in the same player. So don’t. The interaction between module and shell application is communicated by implementing interfaces. If you are going to be using Cairngorm in the base or shell app, then you need to use IoC (inversion of control) to plug a Cairngorm based module into the shell. Specifically, your module will more then likely be using the FrontController to register events/command pairs and it will need to use the shell app’s ServiceLocator to access services. You can easily create an interface that allows you to pass these two items to the module being loaded and if your like me, you can even extend the Module component to make a lot of things happen in the background.
As for calling module events from the shell you can build that all into your interface. All of your delegates, views, ViewHelpers and the modules models will probably be couched in the module, any information that you need from the shell should be provided by IoC.
I will post some code that shows all of this happily working, but for now, I must leave so I can continue to be happily working!
December 19, 2007 at 9:35 am
Just wondering about your solution. It all sounds very simple and straight forward. Will you be sharing with the community the code with which you make this happen?
December 19, 2007 at 11:00 am
Yes, thank you for the reply. I’m currently working on that and a blog entry on view help versus presenter. Unfortunately the next couple of weeks are busy, if there is anything you would like to know directly please let me know.
Happy Holidays!
January 9, 2008 at 1:54 am
When will you write the book on Flex? It’s needed.
April 28, 2008 at 10:03 pm
Ok, I’m REALLY sorry… I have been very busy with the new gig! I will try to get back to this very soon.
June 19, 2008 at 7:17 am
Somehow i missed the point. Probably lost in translation
Anyway … nice blog to visit.
cheers, Entirely.
June 24, 2008 at 5:35 am
Man…I would love to see some code showing your use of Cairngorm and Modules. I’m currently writing a CG app and need to use Modules. The app is basically a portal that will load modules like CRM / Reporting / ETC….my idea was to have the portal base as the module loader and the other apps as separate modules. Does this make sense? I’m not anticipating needing the modules to talk to each other or pass event from one module to another or to/from module loader…does this simplify things for me?? If you have any good reference material or example code, I’d love to see it.
Thanks for the help!