Simple Modular Robotlegs

You know it, when you learn new things looking for documentation or tutorials all over the internet and even if you find potential good source it is some times difficult to focus on the key information while it contains a lot of stuff you do not need at all? Well, there really are some interesting sources for building robotlegs with modules (Modular Robotlegs, DynModules, Modular Tutorial ) but it took me some time to grab only the key functionality. So, I have decided to create the simpliest working project using robotlegs and modules.

First, make sure to download latest Robotlegs framework (v1.4.0) and Robotlegs utilities Modular (provided by Joel Hooks). There is a lot of reading in articles provided before, so I will just skip into some practical things.

It is a required practice to create a new context for an application and for each module. All the created contexts, need to extend ModuleContext class (part of modular utilities) so they are able to communicate. Application context creation flow remains the same as with non mudular apps, it is just required to mapType within startup (interface mapping):


Make sure the module implements correct interface (IModule1). Module contexts need a reference to its parent, fortunately this is handled by modular utilities by default so creating your module context looks something like this:

private var context:Module1Context;

public function set parentInjector(value:IInjector):void
    context = new Module1Context(this, value);

Now, modular utilities offers ModuleMediator that is able to dispatch event (dispatchToModules method) into all ModuleContext-s (listen with moduleCommandMap.mapEvent), but it somehow does not feel right to me to extend all my mediators from ModuleMediator, why should my primitive copmonents mediators be aware that I am using modules?

Luckily there is also a command way of communication between modules, which I like and prefer more. Modular utilities injects IModuleEventDispatcher that is the module dispathcer. Once dispatched here, contexts are able to catch the event (moduleCommandMap.mapEvent). So, this lets me to create in-module-scoped commands (extrending Command) able to communicate between different modules as easy as:

public var moduleDispatcher:IModuleEventDispatcher;
override public function execute():void
    moduleDispatcher.dispatchEvent(new Event("module1Event"));

… and dispatched event can be handled for example by main app context:

moduleCommandMap.mapEvent("module1Event", AppCommand);

A few line of code describing it all, you can download working project with required minimum scripts here.

Tip: If you do not want to mess up with different application domains between modules and parent application, just set ModuleLoader instance applicationDomain to ApplicationDomain.currentDomain, so all loaded modules appears within main app domain:

<mx:ModuleLoader url="Module1.swf" 
    applicationDomain="{ApplicationDomain.currentDomain}" />

Where to go from here:

Leave a comment

Please be polite and on topic. Your e-mail will never be published.