Get event dependency in command

vincent's Avatar


12 Jul, 2011 03:12 PM


I'd like to retrieve the event which has triggered a command.
I use the Inject metadata like this :

[Inject] public var event:Event;

But several events from different classes can trigger this command. If I type my event property in Event, I get an error.
How can I fix this issue?

Thanks. lulu.

  1. 1 Posted by neil on 12 Jul, 2011 03:29 PM

    neil's Avatar

    well, quick fix would be to use a switch statement to check type and then handle accordingly.

    I would say that if these events are of different type, then they should not be firing the same command, and this to me sounds like the beginning (or the middle) of controller tagliatelli ;-(

    The way I would fix this would be to go back to the events dispatched and either dispatch a more appropriate Event, or pick up the One being fired, translate it to the more appropriate one, and dispatch that. Then the command will always get the same Event.

  2. Support Staff 2 Posted by Stray on 12 Jul, 2011 03:35 PM

    Stray's Avatar

    Hi Lulu, yes, this is a bit of a pain I'm afraid.

    There are 2 options.

    One is (if you are compiling from source), to change the line in the commandMap that does the execution, and get it to inject against the specified class, (I've showed the old line commented out here):

    protected function routeEventToCommand(event:Event, commandClass:Class, 
                oneshot:Boolean, originalEventClass:Class):Boolean
            if (!(event is originalEventClass)) return false;
            //execute(commandClass, event);
            execute(commandClass, event, originalEventClass);
            if (oneshot) unmapEvent(event.type, commandClass, originalEventClass);
            return true;

    This will automatically inject against Event unless you've specified the event type in the commandMap mappings, so if you want strongly typed events you would need to do:

    commandMap.mapEvent(SomeEvent.BLAH, Command, SomeEvent);

    and for the ones that you want to inject against Event:

    commandMap.mapEvent(SomeEvent.BLAH, Command);

    Alternatively, you can create commands which are bound to the specific event type that fires them, and then execute the shared command functionality from within them.

    This is quite unusual - what's your use case? I'm only asking because I've only come across the problem myself a couple of times.

    let me know if that's not clear...


  3. 3 Posted by neil on 12 Jul, 2011 03:44 PM

    neil's Avatar

    Oh I see, I hadn't quite picked up that subtlety,
    over to stray...

  4. 4 Posted by vincent on 12 Jul, 2011 05:24 PM

    vincent's Avatar

    Hi guys,

    yes, I can create a command for each event type but, in my logic, I'd like to trigger a unique command to execute some similar tasks.
    For example, in views, I'd trigger navigation events to switch between states and I'd like to get just one command which would tell the right state to display.

    Thanks for replies. lulu.

  5. Support Staff 5 Posted by Ondina D.F. on 02 Nov, 2011 05:59 PM

    Ondina D.F.'s Avatar

    Hi Vincent,
    Thank you for posting.
    Feel free to reopen this discussion if you feel the answer(s) to be dissatisfactory or if you need more help with this issue, otherwise we'll consider the thread closed. Please open new threads for new issues.

  6. Ondina D.F. closed this discussion on 02 Nov, 2011 05:59 PM.

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