tag:robotlegs.tenderapp.com,2009-10-18:/discussions/robotlegs-2/17748-different-mediators-for-same-viewRobotlegs: Discussion 2016-11-23T13:16:41Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/412740922016-11-22T11:18:14Z2016-11-22T11:18:14ZDifferent mediators for same view Class<div><p>mediatorMap.mapMatcher and interfaces are your friends in this
case.</p>
<p>A ViewWithManyMediators would implement an interface, say
IDoStuff.</p>
<p>The mappings would look like this:</p>
<pre>
<code>mediatorMap
.mapMatcher(new TypeMatcher().allOf(IDoStuff, ViewWithManyMediators))
.toMediator(FirstMediator);
mediatorMap
.mapMatcher(new TypeMatcher().allOf(IDoStuff, ViewWithManyMediators))
.toMediator(SecondMediator);</code>
</pre>
<p><em>But</em>, FirstMediator and SecondMediator would both
mediate each of the 2 instances of the View.</p>
<p>If you want FirstMediator to mediate only the first instance of
the View, and SecondMediator only the second instance of the View,
you need to make use of guards:</p>
<pre>
<code>mediatorMap
.mapMatcher(new TypeMatcher().allOf(IDoStuff, ViewWithManyMediators))
.toMediator(FirstMediator).withGuards(FirstGuard);
mediatorMap
.mapMatcher(new TypeMatcher().allOf(IDoStuff, ViewWithManyMediators))
.toMediator(SecondMediator).withGuards(SecondGuard);</code>
</pre>
<p>As an example, you could approve the mediation of the first
instance of ViewWithManyMediators by the FirstMediator only if the
view's name is "firstInstance". Similar for the second instance, if
the view's name is "secondInstance", SecondMediator would be the
one mediating the second instance of the view added to the
stage.</p>
<p>So, ViewWithManyMediators exposes a setter and getter for the
viewName.<br>
When you add the 2 views to the stage, you set their names
accordingly, "firstInstance" and "secondInstance".</p>
<p>A guard would look like this:</p>
<pre>
<code>public class FirstGuard
{
[Inject] public var view:IDoStuff;
public function approve():Boolean
{
return view.viewName=="firstInstance";
}
}
public class SecondGuard
{
[Inject] public var view:IDoStuff;
public function approve():Boolean
{
return view.viewName=="secondInstance";
}
}</code>
</pre>
<p>IDoStuff should, of course, contain the getter and setter for
the viewName, so that the view can implement them.</p>
<p>When the mediators are about to be created, the guards will make
sure that each instance of the view on stage will have different
mediators.</p>
<p>Both mediators are injected with IDoStuff, as are the 2 guards
as well.<br>
[Inject] public var view:IDoStuff;</p>
<p>The 2 instances of the view can exhibit a common behaviour or
different behaviours, dictated by the use of one or more
interfaces. Here is a link to an example that shows how to use
interfaces to create and mediate behaviours, in case you are
interested in the subject:</p>
<p><a href="http://knowledge.robotlegs.org/discussions/robotlegs-2/12476-rl2-bootstrap-command-chain#comment_35099538">
http://knowledge.robotlegs.org/discussions/robotlegs-2/12476-rl2-bo...</a></p>
<p>The zip file is at the end of my answer.</p>
<p>About guards:<br>
<a href="https://github.com/robotlegs/robotlegs-framework/blob/master/src/robotlegs/bender/framework/readme-guards.md">
https://github.com/robotlegs/robotlegs-framework/blob/master/src/ro...</a></p>
<p>About mapMatcher:<br>
<a href="https://github.com/robotlegs/robotlegs-framework/tree/master/src/robotlegs/bender/extensions/mediatorMap#making-mappings">
https://github.com/robotlegs/robotlegs-framework/tree/master/src/ro...</a></p>
<p>I hope that helps.<br>
Ondina</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/412740922016-11-22T15:12:35Z2016-11-22T15:12:35ZDifferent mediators for same view Class<div><p>As always, great answer!</p></div>kamcknigtag:robotlegs.tenderapp.com,2009-10-18:Comment/412740922016-11-23T13:16:37Z2016-11-23T13:16:37ZDifferent mediators for same view Class<div><p>I'm glad it was helpful :)</p></div>Ondina D.F.