tag:robotlegs.tenderapp.com,2009-10-18:/discussions/problems/57-mediators-and-multiple-instances-of-the-viewRobotlegs: Discussion 2018-10-18T16:35:09Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/12101762010-03-13T19:48:09Z2010-03-13T19:48:09ZMediators and multiple instances of the view.<div><p>Hi,</p>
<p>I have a view that is added at run time. Most of the times, I
have several instances of that view. In my context, I'm mapping the
mediator:<br></p>
<pre>
<code> mediatorMap.mapView(MyView, MyViewMediator);</code>
</pre>
<p>My problem is that when one of the views dispatch an event, all
mediator response to it. I want that just the mediator of the
instance that dispatched the event is able to listen to it.</p>
<p>I can use ids, to check id the mediators match the id of the
event, but it should be a better way to do it.</p>
<pre>
<code> [Inject]
public var myView:MyView;
private function handleEvent(event:CustomEvent):void{
if(myView.viewid == event.viewid){
//do something
}
}</code>
</pre></div>oscartag:robotlegs.tenderapp.com,2009-10-18:Comment/12101762010-03-13T20:15:32Z2010-03-13T20:15:32ZMediators and multiple instances of the view.<div><p>Same question for Commands. If a command is going to dispatch an
event that will trigger an action in a mediator of an specific
instance of a view, how do I manage to have only that mediator to
pick up that event?</p>
<p>Tks.</p></div>oscartag:robotlegs.tenderapp.com,2009-10-18:Comment/12101762010-03-13T20:28:29Z2010-03-13T20:28:29ZMediators and multiple instances of the view.<div><p>Your mediators shouldn't all get these events. As each of
the<br>
mediators is injected with a reference to the one view it is<br>
mediating, you should be able to listen to events from that view
and<br>
really only get those, not the events from other views of the
same<br>
type.</p>
<p>The only simple explanation for that behavior to occur is if
your<br>
views are nested into one another on the display list and your
events<br>
are bubbling.<br>
If that's the case, you'll have to either change the event
dispatching<br>
to make them non-bubbling, or check the event.target property
to<br>
verify that the event was dispatched by the right view.</p>
<p>As for events dispatched from commands: Commands shouldn't
normally<br>
act on mediators at all. Mediators should instead listen to events
on<br>
the event bus and react to them. These events might of course
be<br>
dispatched by commands if that makes sense in the context of
your<br>
application.</p></div>Till Schneidereittag:robotlegs.tenderapp.com,2009-10-18:Comment/12101762010-03-13T22:29:19Z2010-03-13T22:29:19ZMediators and multiple instances of the view.<div><p>Totally right. my views were nested into others views and the
events were bubbling.</p>
<p>Tks.</p></div>oscar