Re-disptaching an event to the shared eventDispatcher

dedorris's Avatar


10 Oct, 2014 10:52 PM

Let's say that I have a Mediator that listens for an event from its View.
For this example, I will use the familiar MovieClip "enterFrame" event, but my question applies to any event.

addViewListener(Event.ENTER_FRAME, enterFrameHandler);

My Mediator defines "enterFrameHandler" using the standard ActionScript event handler signature, with the Event object as its only parameter.

If I want my Mediator to dispatch an Event to the shared eventDispatcher, I can use the Mediator's "dispatch(event)" convenience method (which calls "dispatchEvent(event)" on the shared eventDispatcher).

private function enterFrameHandler(event:Event):void 

Common sense suggests that since the "dispatch" method has the required method signature, I can directly provide the "dispatch" method as the event handler callback (if all I want to do is re-dispatch the same Event object on the shared eventDispatcher).

addViewListener(Event.ENTER_FRAME, dispatch);

A quick test confirms that this works.

But I am wondering, will using "dispatch" as my event handler callback cause any bad side effects?

  1. Support Staff 1 Posted by Ondina D.F. on 13 Oct, 2014 12:13 PM

    Ondina D.F.'s Avatar

    What you've discovered, is a functionality that has been there since the beginnings of rl :)
    Having the Mediator's dispatch() method as the event handler is a very common usage.

    But I am wondering, will using "dispatch" as my event handler callback cause any bad side effects?

    The dispatch(event); in your enterFrameHandler() is Mediator's dispatch():

    There are no disadvantages per se, when re-dispatching an event to rl's event bus. What kind of side effects were you expecting to come from this?

    Perhaps you are referring to issues related to listening directly to a view (garbage collection issues/destroying mediators) or its subcomponents (for example non-bubbling events)? Or, to issues caused by dispatching custom events that don't override Event's clone() method()? Or, by forgetting to set the eventClass parameter ( the third optional parameter in addViewListener/addContextListener), which is meant to create a type safe mapping?
    Or, when you map a command to something like a MouseEvent.CLICK, that can be re-dispatched from several mediators? Or, the fact that if you don't dispatch custom events from views, you'd have to change the addViewListener in your mediators, if a MouseEvent is replaced by a Touch event in your views (for example if you build a desktop and a mobile version for your app) ?

  2. Ondina D.F. closed this discussion on 05 Dec, 2014 11:51 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