tag:robotlegs.tenderapp.com,2009-10-18:/discussions/robotlegs-2/12457-re-disptaching-an-event-to-the-shared-eventdispatcherRobotlegs: Discussion 2014-12-05T11:51:21Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/349141672014-10-10T22:52:04Z2014-10-10T22:52:28ZRe-disptaching an event to the shared eventDispatcher<div><p>Let's say that I have a Mediator that listens for an event from
its View.<br>
For this example, I will use the familiar MovieClip "enterFrame"
event, but my question applies to any event.</p>
<pre>
<code>addViewListener(Event.ENTER_FRAME, enterFrameHandler);</code>
</pre>
<p>My Mediator defines "enterFrameHandler" using the standard
ActionScript event handler signature, with the Event object as its
only parameter.</p>
<p>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).</p>
<pre>
<code>private function enterFrameHandler(event:Event):void
{
dispatch(event);
}</code>
</pre>
<p>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).</p>
<pre>
<code>addViewListener(Event.ENTER_FRAME, dispatch);</code>
</pre>
<p>A quick test confirms that this works.</p>
<p>But I am wondering, will using "dispatch" as my event handler
callback cause any bad side effects?</p></div>dedorristag:robotlegs.tenderapp.com,2009-10-18:Comment/349141672014-10-13T12:13:21Z2014-10-13T12:13:21ZRe-disptaching an event to the shared eventDispatcher<div><p>What you've discovered, is a functionality that has been there
since the beginnings of rl :)<br>
Having the Mediator's dispatch() method as the event handler is a
very common usage.</p>
<blockquote>
<p>But I am wondering, will using "dispatch" as my event handler
callback cause any bad side effects?</p>
</blockquote>
<p>The dispatch(event); in your enterFrameHandler() is Mediator's
dispatch():</p>
<p><a href=
"https://github.com/robotlegs/robotlegs-framework/blob/master/src/robotlegs/bender/bundles/mvcs/Mediator.as#L94">
https://github.com/robotlegs/robotlegs-framework/blob/master/src/ro...</a></p>
<p>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?</p>
<p>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?<br>
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) ?</p></div>Ondina D.F.