Currently using robotlegs-utilities-Modular but moving to RL2 how to achieve a commandMap, moduleCommandMap

Sharif El-Borgi's Avatar

Sharif El-Borgi

15 Oct, 2012 12:37 PM

Being exposed to the event dispatcher for the context and module seperatly is what i would like to achieve.

I have looked at this example which is great when defining seperate eventDispatchers for global and moduleOnly modes, but how do i then inject these into a commandMap:ICommandMap and moduleCommandMap:ICommandMap.

Can someone please help.

  1. 1 Posted by Sharif El-Borgi on 15 Oct, 2012 12:39 PM

    Sharif El-Borgi's Avatar

    ... to add what i want to be able to achieve is the same as i had before, which was to be able to dispatch an event within and context and to the other modules and not the context im dispatching from.


  2. Support Staff 2 Posted by Ondina D.F. on 15 Oct, 2012 01:53 PM

    Ondina D.F.'s Avatar

    Hello Sharif,

    ... to add what i want to be able to achieve is the same as i had before, which was to be able to dispatch an event within and context and to the other modules and not the context im dispatching from.

    From my experience, you can define the “channels” (dispatchers) in the parent context of 3 sibling modules like this:

    context.extend(new ScopedEventDispatcherExtension("global", "channelOne", "channelTwo"));

    • If “global” is the common channel for the parent and all modules, then wherever you inject it like this:

    [Inject(name="global")] public var globalDispatcher:IEventDispatcher;

    you’ll have to disptach the events like this:
    globalDispatcher.dispatchEvent(new SomePopUpEvent(SomeEvent.SOMETHING_HAPPENED));

    and the shell and modules would listen to the event like this:
    eventMap.mapListener(globalDispatcher, SomeEvent.SOMETHING_HAPPENED, onSomethingHappened, SomeEvent);

    If one of the Modules is not interested in sharing the “global” dispatcher, it won’t inject it in the first place, or it won’t listen for that event on this dispatcher.

    Also look at the settings of the ModularityExtension. Here a discussion about the different settings and their consequences for the modules:

    • If 2 Modules want to communicate through “channelOne”, they’d inject a dispatcher like this: [Inject(name="channelOne")] public var someDispatcher:IEventDispatcher;

    add a listener:
    eventMap.mapListener(someDispatcher, SomeEvent.SOMETHING_HAPPENED, onSomethingHappened, SomeEvent);

    and dispatch the event:
    someDispatcher.dispatchEvent(new SomeEvent(SomeEvent.SOMETHING_HAPPENED));

    • Within one context the shared dispatcher of that context will be used as usual.

    In Mediators like this:
    addContextListener(SomeEvent.SOMETHING_HAPPENED, onSomethingHappened, SomeEvent);
    dispatch(new SomeEvent(SomeEvent.SOMETHING_HAPPENED));

    In commands you’d have to inject the dispatcher:
    [Inject] public var localDispatcher:IEventDispatcher;

    localDispatcher.dispatchEvent(new SomeEvent(SomeEvent.SOMETHING_HAPPENED));

    I hope I understood your question correctly.


  3. 3 Posted by Sharif El-Borgi on 15 Oct, 2012 02:01 PM

    Sharif El-Borgi's Avatar

    Thank you for the prompt reply. I gathered i could do as you explained, what i was looking for was previously in RL1 you could use and moduleCommandMap .... to achieve this, do i create a new instance of IEventCommandMap (i.e var commandMap = new EventCommandMap(globalDispatcher) and var moduleCommandMap = new EventCommandMap(moduleChannelDispatcher) ) then pop them into the injector of the context? if so how do i then pass these into each context? using the ModularityExtension - i guess i would like to use the EventCommandMap as a wrapper for manageing mapping and unmapping events to Commands.


  4. Support Staff 4 Posted by Ondina D.F. on 15 Oct, 2012 02:24 PM

    Ondina D.F.'s Avatar

    No problem.
    Hehe, it seems I misunderstood your question though.
    Let’s see if I get it right this time.

    I guess you want to dispatch an event from a module to trigger a command mapped in the shell’s context?
    I’m not sure about the best solution in this case. I hope Shaun or Stray will answer this.

    I only can tell you how I solved such a situation:
    In the Shell’s context:, SomePopUpEvent).toCommand(SomePopUpCommand);

    In the Module’s Context:
    parentDispatcher=injector.parentInjector.getInstance(IEventDispatcher);, "somePopUpDispatcher").toValue(parentDispatcher);

    In the class that wants to open the shell’s popup:

    [Inject(name="somePopUpDispatcher")] public var somePopUpDispatcher:IEventDispatcher;

    somePopUpDispatcher.dispatchEvent(new SomePopUpEvent (SomePopUpEvent.OPEN_POPUP));

  5. 5 Posted by Sharif El-Borgi on 15 Oct, 2012 02:30 PM

    Sharif El-Borgi's Avatar

    Yeah that sounds like a midterm solution - i think i will go with that for time being, hopefully Shaun or Stray can shed some light here.

  6. 6 Posted by Sharif El-Borgi on 15 Oct, 2012 02:31 PM

    Sharif El-Borgi's Avatar

    oh! and thank you! :) much appreciated Ondina!

  7. 7 Posted by Sharif El-Borgi on 15 Oct, 2012 02:35 PM

    Sharif El-Borgi's Avatar

    Thanks Ondina!

  8. Support Staff 8 Posted by Ondina D.F. on 15 Oct, 2012 02:39 PM

    Ondina D.F.'s Avatar

    I wasn't of much help, but, of course, you're welcome, Sharif:)

  9. 9 Posted by Sharif El-Borgi on 15 Oct, 2012 02:49 PM

    Sharif El-Borgi's Avatar

    Just had a thought.... I could get the EventCommandMap from the parentInjector right?

    that way i can use it in the Modules too to map events from other modules :)

    or am i way off?

  10. Support Staff 10 Posted by Ondina D.F. on 15 Oct, 2012 02:56 PM

    Ondina D.F.'s Avatar

    I think you can do that. Try it out and see if it's what you really want:)
    And don't forget to report back.

  11. Support Staff 11 Posted by Ondina D.F. on 15 Oct, 2012 06:03 PM

    Ondina D.F.'s Avatar

    My findings:

    Shell Context:
    shellDispatcher=injector.getInstance(IEventDispatcher);, "hello").toValue(shellDispatcher);

    [1]module’s command mapped in shell’s context

    //, SomeEvent).toCommand(ModuleCommand);

    [2], SomeEvent).toCommand(ShellCommand);

    In a Class

    [Inject(name="hello")] public var helloDispatcher:IEventDispatcher;

    //this triggers the Module’s command:

    helloDispatcher.dispatchEvent(new SomeEvent (SomeEvent.TRIGGER_MODULE_COMMAND));

    Module Context:
    var parentCommandMap:IEventCommandMap=injector.parentInjector.getInstance(IEventCommandMap);

    [1] module’s command mapped in module’s context, using shell’s commandmap, SomeEvent).toCommand(ModuleCommand);

    In a Class:
    [Inject(name="hello")] public var helloDispatcher:IEventDispatcher;

    //triggers Shell’s command:

    helloDispatcher.dispatchEvent(new SomeEvent(SomeEvent.TRIGGER_SHELL_COMMAND));

    Till tomorrow...

  12. 12 Posted by Sharif El-Borgi on 17 Oct, 2012 10:34 AM

    Sharif El-Borgi's Avatar

    Hi Ondina,

    Im still having trouble with the modules having there own eventDispatcher. So currently im dispatching an event from within a module, this is getting picked up by the Shell commandMap ... where as i would like to keep this in the module.

    Steps i would like:

    1. Dispatch event from mediator in module
    2. mapped event in Module command map picks this up and executes the command mapped.
    3. command updates module model
    4. module model dispatches the event through event dispatcher
    5. module mediator sees event and updates a label

    whats happening :

    1. dispatch event from mediator in module
    2. gets picked up by the shells command map
    3. updates the shell model
    4. mediator in module picks up the event dispatched from application model
    5. and module view changes

    in this example i have used a common event.

    Not sure why this is happening? and not sure how to seperate the two.

    ive added link to github for this project


  13. Support Staff 13 Posted by Ondina D.F. on 17 Oct, 2012 10:37 AM

    Ondina D.F.'s Avatar

    Hi Sharif,

    I'll take a look at your example and come back with answers (or questions).


  14. Support Staff 14 Posted by Ondina D.F. on 17 Oct, 2012 11:50 AM

    Ondina D.F.'s Avatar

    I tried out your example, and made these modifications:

    parentDispatcher=injector.getInstance(IEventDispatcher);, "hello").toValue(parentDispatcher);


    [Inject (name="hello")]
    public var helloDispatcher:IEventDispatcher;
    //dispatch the value as a payload
    helloDispatcher.dispatchEvent(new ChangeLabelEvent(ChangeLabelEvent.CHANGE_LABEL_HAPPENED, value));


    [Inject (name="hello")]
    public var helloDispatcher:IEventDispatcher;
    override public function initialize():void {
    //listen on the shared dispatcher:
    eventMap.mapListener(helloDispatcher,ChangeLabelEvent.CHANGE_LABEL_HAPPENED, onChangeLabelHappened);
    private function onChangeLabelHappened(e:ChangeLabelEvent):void {
    view.lblSomeText.text = e.message;

    In MainMediator do the same as in BlogViewMediator, and in ApplicationModel the same as in BlogModel.


    public function ChangeLabelEvent(type:String, message:String=null, bubbles:Boolean=false,  cancelable:Boolean=false) {
        super(type, bubbles, cancelable);
    public function get message():String
        return _message;

    With these modifications, when BlogModel or ApplicationModel dispatch ChangeLabelEvent, both, BlogViewMediator and MainMediator, react to the event and the payload is the value set in the model dispatching the event.

    Not sure, if that’s what you want, though. If it’s not what you wanted, please explain again using the names of the classes, so I can follow better.

  15. 15 Posted by Sharif El-Borgi on 17 Oct, 2012 12:01 PM

    Sharif El-Borgi's Avatar

    This is very helpful. However, im trying to really make use of the CommandMap again like in RL1. So i have a command in the Shell and a command in the Blog Module. I then would like to have a command map for each of these contexts. each responding to there own events. So when the event is dispatched via its eventDispatcher in its context, it is reacted to by the correct command map.

  16. Support Staff 16 Posted by Ondina D.F. on 17 Oct, 2012 12:13 PM

    Ondina D.F.'s Avatar

    dispatch event from mediator in module

    dispatch(new ChangeLabelEvent(ChangeLabelEvent.CHANGE_LABEL_REQUEST));

    gets picked up by the shells command map

    Actually, it gets picked up by the BlogChangeLabelCommand. Use traces or log to see it.

    updates the shell model

    Actually, it updates BlogModel.

    mediator in module picks up the event dispatched from application model

    Nope, it is the one from BlogModel, but I found the culprit:
    You are injecting ApplicationModel into BlogMediator
    [Inject] public var model:ApplicationModel;

    You surely want BlogModel instead, right?
    Inject BlogModel, and it will work as you wanted it to work:) No other modifications are needed.

  17. 17 Posted by Sharif El-Borgi on 17 Oct, 2012 01:41 PM

    Sharif El-Borgi's Avatar

    Thank you! thats great. Let me go back to the example app and check this further. Thanks again for your prompt reply.

  18. Support Staff 18 Posted by Ondina D.F. on 17 Oct, 2012 04:58 PM

    Ondina D.F.'s Avatar

    My pleasure!

  19. Ondina D.F. closed this discussion on 23 Oct, 2012 10:44 AM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts


? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac