tag:robotlegs.tenderapp.com,2009-10-18:/discussions/questions/987-in-an-mvc-framework-can-a-mediator-be-initialized-in-the-view-itself-so-that-as-soon-as-view-is-instantiatedRobotlegs: Discussion 2018-10-18T16:35:41Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/177989922012-08-03T06:58:42Z2012-08-03T06:58:42ZIn an MVC framework, can a mediator be initialized in the view itself. So that as soon as view is instantiated...<div><p>Why a mediator has either to depend upon manual creation or only
after view's addchild is triggered ( automatic) ?<br>
In an MVC framework, can a mediator initialized in the view itself.
So that as soon as view is instantiated, the constructor creates
the mediator of the view immediately. And then, with "getMediator"
method, the mediator instance of that view can be accessed ?</p></div>vishwas.gagranitag:robotlegs.tenderapp.com,2009-10-18:Comment/177989922012-08-03T13:49:44Z2012-08-03T13:53:07ZIn an MVC framework, can a mediator be initialized in the view itself. So that as soon as view is instantiated...<div><p>Hi Vishwas,</p>
<p>With Robotlegs, when you ask "can...?" then in general the
answer is "yes": its flexibility allows you to do almost anything
with little to no modification. However, following best practices
is another thing. :)</p>
<p>The main role of the Mediator in Robotlegs is to separate
application communication from the view implementation. It passes
messages (events, signals) from the view component to the rest of
the app and vice versa. This allows the view to become independent
from the app, increasing reusability! So, to answer your question:
a "getMediator" method doesn't make sense, because a view shouldn't
know about what manages it. Here's a nice article by Stray about
small and focused Mediators: <a href=
"http://www.xxcoder.net/my-mailman-doesnt-open-my-mail">http://www.xxcoder.net/my-mailman-doesnt-open-my-mail</a>
(and an interesting discussion in the comments as well!)</p>
<p>Regarding view instantiation and adding it to the stage: a
pattern that I started to like is creating a Mediator for the
context view. This Mediator can listen to the correct application
events and create and add the views to the stage (and later remove
them) when necessary. This, to me, feels more logical than doing
that work from a Command, since Commands belong to the Controller
tier. 2 notes about a "ContextViewMediator":</p>
<ul>
<li>
<p>You can use <code>mediatorMap.mapView(contextView,
ContextViewMediator);</code> to map the view to its Mediator.</p>
</li>
<li>
<p>Always map the context view last, because its
<code>onRegister</code> method will be called immediately.</p>
</li>
</ul>
<p>I hope this has given you a push in the right direction.
Definitely come back if you have more questions!</p>
<p>Abel</p></div>Abel de Beertag:robotlegs.tenderapp.com,2009-10-18:Comment/177989922012-08-15T15:31:16Z2012-08-15T15:31:16ZIn an MVC framework, can a mediator be initialized in the view itself. So that as soon as view is instantiated...<div><p>Thanks, that was helpful .</p></div>vishwas.gagrani