CommandMap Generic Events?

Josh's Avatar


25 Oct, 2012 12:01 AM

I am wondering if RL supports (or for that matter, recommends) functionality within the Context for mapping generic events of a certain type to a Command. I suppose the usage example would be:

External service (Actor) throws events of a given type, like "ApplicationStateChangeEvent.APPLICATION_LOADING" , and there might be (and in fact, are) upwards of 40 different specific events (the "APPLICATION_LOADING" portion of that string above). I would like to have something analogous to:
commandMap.mapEvent(ApplicationStateChangeEvent.*, StateChangeCommand);

instead of:
commandMap.mapEvent(ApplicationStateChangeEvent.APPLICATION_LOADING, ApplicationLoadingCommand);

Any thoughts on this? What I'd like to do is handle the switching on the specifics done in the Command rather than in the Context.



  1. 1 Posted by Paul Robertson on 25 Oct, 2012 12:23 AM

    Paul Robertson's Avatar

    The only way to do that (that I know of) would be to map each event type
    individually to the command.

    That sounds difficult, which is probably as it should be. The point of
    using the command pattern (with individual small command classes) rather
    than a monolithic controller class is that each command does just one
    thing. (See "Single Responsibility Principle".) That way it's easier to
    understand and easier to test and debug. (If you work with a team, it has
    the added benefit that you're less likely to edit a file at the same time
    as another developer, so you'll have fewer merge conflicts.)

    Of course you may disagree, or have a good reason for not wanting to use
    the pattern that Robotlegs (or more specifically, the default MVCS
    implementation) encourages. In that case the MVCS implementation probably
    isn't the right thing for you because it's designed to support a certain
    pattern. You'd want to write an alternative command map to use instead.


  2. 2 Posted by Josh on 25 Oct, 2012 04:33 PM

    Josh's Avatar

    Thanks, Paul. It's funny, that's the exact comment I made to my co-worker, "They did a pretty good job of making poor practices painful enough to accomplish so as to discourage them". :) I totally get the whole "Single Responsibility Principle", but I absolutely hate having the main Context class polluted with (in this case) dozens upon dozens of CommandMap declarations. The other thing I said when I wrote this post was, "I'm sure the majority of answers I'm going to get will be, 'if you don't like all those CommandMap statements in the main Contaxt, pull them out into a separate bootstrap file'. Harumph, foiled again. :)

    I guess I'll stick with convention and have all of the Command files, but it really feels like overkill in this case. I'll do it though. :)

  3. Support Staff 3 Posted by Till Schneidere... on 25 Oct, 2012 05:02 PM

    Till Schneidereit's Avatar

    Also note that, the way event dispatching in Flash works, the only
    place where you could implement such a functionality would be in the
    `dispatch` method. There, you could inspect the object, see if it is
    of the right instance and invoke your handler based on that. Perhaps
    the easiest way to do so would be to take the event type and
    temporarily add your handler as a listener to it before calling the
    real `dispatch` function. You'd have to override the context's event
    dispatcher for that.

  4. Ondina D.F. closed this discussion on 05 Nov, 2012 09:41 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