tag:robotlegs.tenderapp.com,2009-10-18:/discussions/problems/502-how-can-i-use-the-same-mediator-classunchanged-for-different-view-classesRobotlegs: Discussion 2018-10-18T16:35:38Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/143313042012-03-08T10:07:19Z2012-03-08T10:07:20ZHow can I use the same mediator class(unchanged) for different view classes?<div><p>I have a contextview mediator class that is same for all my
apps(for external communication) but every time I create a new app
I need to change the view class name inside the mediator class</p>
<p>[Inject] public var view:NewAppMain;</p>
<p>I'd like to use this mediator class unchanged, is it possible?
Maybe using interface or by extending?</p></div>freqnctag:robotlegs.tenderapp.com,2009-10-18:Comment/143313042012-03-08T10:24:15Z2012-03-08T10:24:15ZHow can I use the same mediator class(unchanged) for different view classes?<div><p>I would create a parent mediator class, then subclass this in
order to create a mediator for each view. This way all the main,
repeatable work in handled by the parent. Then if one of your
mediators later needs it's own specific piece of functionality,
then you add that to the child mediator for that view.</p>
<p>Stephen</p></div>stephenadams1tag:robotlegs.tenderapp.com,2009-10-18:Comment/143313042012-03-08T10:29:10Z2012-03-08T10:29:11ZHow can I use the same mediator class(unchanged) for different view classes?<div><p>ok but there are references to the view class inside the code,
it has to be already in the parent...</p></div>freqnctag:robotlegs.tenderapp.com,2009-10-18:Comment/143313042012-03-08T12:37:43Z2012-03-08T12:37:44ZHow can I use the same mediator class(unchanged) for different view classes?<div><p>I tried it like this - the contextview class extends a class
that is injected to the mediator - but got errors when trying to
map the subclass...</p></div>freqnctag:robotlegs.tenderapp.com,2009-10-18:Comment/143313042012-03-08T14:06:22Z2012-03-08T14:06:22ZHow can I use the same mediator class(unchanged) for different view classes?<div><p>Assuming you use RL1, I would recommend the following:</p>
<p>Have your main class implement an interface, say
IContextView.</p>
<p>When you map the contextView to its mediator in the context, use
the "injectViewAs" parameter:
<code>mediatorMap.mapView(contextView, ContextViewMediator,
IContextView);</code></p>
<p>Now you can inject the contextView in your mediator like this:
<code>[Inject] public var view:IContextView;</code></p>
<p>What's nice about this approach is that you can use the custom
methods you set up in your interface through the injected view
property, but you're also able to use your Main app's
DisplayObjectContainer functionality (e.g.
<code>addChild();</code>) through the contextView property that is
injected into the default Mediator, if necessary.</p></div>Abel de Beertag:robotlegs.tenderapp.com,2009-10-18:Comment/143313042012-03-08T15:09:40Z2012-03-08T16:06:01ZHow can I use the same mediator class(unchanged) for different view classes?<div><p>I have tried this also but I cannot access to the contextView
stage that is crucial to my apps...</p>
<p>override public function onRegister():void</p>
<pre>
<code> {
view.stage.addEventListener ...</code>
</pre>
<p>gives</p>
<p>1119: Access of possibly undefined property stage through a
reference with static type
net.unrealcasino.common.views:IContextView</p>
<p>and it seems that I cannot use even the DisplayObjectContainer
functionality?</p>
<p>1061: Call to a possibly undefined method dispatchEvent through
a reference with static type
net.unrealcasino.common.views:IContextView</p></div>freqnctag:robotlegs.tenderapp.com,2009-10-18:Comment/143313042012-03-08T16:18:36Z2012-03-08T16:18:36ZHow can I use the same mediator class(unchanged) for different view classes?<div><p>If you look, you'll see you own</p>
<blockquote>
<p>net.unrealcasino.common.views:IContextView</p>
</blockquote>
<p>So if you make that interface extend IEventDispatcher then
you'll be able to dispatch events, and if you add a getter for
stage to that interface you'll be able to access the stage.</p>
<p>The other option is to use the contextView property rather than
your view</p>
<p>eg:</p>
<p>contextView.stage.addEventListener...</p>
<p>Stray</p></div>Straytag:robotlegs.tenderapp.com,2009-10-18:Comment/143313042012-03-09T08:05:42Z2012-03-09T08:05:43ZHow can I use the same mediator class(unchanged) for different view classes?<div><p>Of course contextView is available in the mediator!<br>
Thanks guys, this is the solution I was trying to figure out!</p></div>freqnc