Dispatching Several Events

oscar's Avatar


17 Mar, 2010 11:50 PM


In one of my mediators, I have a method that dispatches 3 events. All 3 events trigger different commands. All commands perform some work on the model. The first two commands do additions in the model. The third command check some values in the model (values updated by the first two commands) and dispatches events that are been listed by the mediator.

My problem is that the third event and command are been executed before the first two commands are done with their job.
In other words, I need to make sure that the events / commands perform their job in the order I dispatch them.
How can I control this?

Thanks in advance.

  1. 1 Posted by Jonny Reeves on 18 Mar, 2010 10:10 AM

    Jonny Reeves's Avatar

    Hi Osc,

    Without seeing the code in question it's a little tricky to make an informed opinion, however I am wondering why you need three separate Commands, why can't a single Command perform the required additions and then check the values afterwords?

  2. Support Staff 2 Posted by Till Schneidere... on 18 Mar, 2010 10:19 AM

    Till Schneidereit's Avatar

    Sounds like at least one of your commands start some asynchronous
    activity, whose completion you want to wait on.

    If that's not the case, then something else is going on: Synchronous
    actions can't ever be completed out of order in AS3. So if command 1
    to 3 are invoked in order, they'll definitely complete in that same
    order, too. Same goes for dispatched events, so if it seems like all
    of your events/ commands *should* complete in order while in fact they
    do not, there has to be a bug somewhere else that causes that

    If, on the other hand, your problem is indeed caused by the desire to
    wait for an asynchronous action to complete, you should probably use a
    service for that.

    See Stray's awesome decision graph about what should go where for some
    hints about what should be in a service:

  3. Support Staff 3 Posted by Shaun Smith on 18 Mar, 2010 01:54 PM

    Shaun Smith's Avatar

    Also, it sounds to me like you are driving application logic from your mediator. This is usually a sign of a troublesome design. In general, mediators should merely relay significant view events through to the application. You should simply be telling the application what happened to the view, and this should translate into a single event. It's up to the application to decide what to do in response to that event.

  4. 4 Posted by oscar on 18 Mar, 2010 02:33 PM

    oscar's Avatar

    Thanks all.
    Yes, the first command was adding a component to the display list, when it's created (and itits model) a reference to the model is added to "the master model". That was completed after the third command executed.

    The reason why I have to dispatch three events is that I'm already using those events in the application, and I don't want to re-write that functionality just to avoid duplicated code.

    Thanks guys!

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