tag:robotlegs.tenderapp.com,2009-10-18:/discussions/solutions/7-mediator-for-viewstackRobotlegs: Discussion 2018-10-18T16:35:15Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/25714952010-11-03T16:59:58Z2010-11-03T16:59:59ZMediator for viewstack<div><p>I'd love to see a best practice for how to manage mediators of viewstack children. Setting the viewstack's creationPolicy to all doesn't sound like the best solution as far as performance, but definitely works.</p>
<p>I'm experimenting with the following.:</p>
<p>Code on Parent Mediator of ViewStack<br />
`</p>
<pre><code> override public function onRegister():void
{
// Manually Mediate ViewStack
eventMap.mapListener(view.viewstack, IndexChangedEvent.CHANGE, handleViewStackChange);
}
/**
* Manually Mediate ViewStack Children
*/
private function handleViewStackChange(e:IndexChangedEvent):void
{
var oldView:Object = view.viewstack.getChildAt(e.oldIndex);
mediatorMap.removeMediatorByView( oldView );
mediatorMap.createMediator( view.viewstack.selectedChild );
}`</code></pre>
<p>The ApplicationContext has each child of the viewstack manually mediated as follows:<br />
<code>mediatorMap.mapView( MyChildView, MyChildViewMediator, null, false, false );</code></p>
<p>This currently is not working because the mediator is being created before the child view when the index changes. Thoughts on this?</p></div>Brady Whitetag:robotlegs.tenderapp.com,2009-10-18:Comment/25714952010-11-04T02:22:12Z2010-11-04T02:22:12ZMediator for viewstack<div><p>Howdy,</p>
<p>Auto-mediation (the default) is designed to work <em>with</em> deferred instantiation. Setting "creationPolicy" to "all" and/or manually creating mediators on viewstack-index-change defeats the point. From the very first RL <a href="http://examples.robotlegs.org/helloflex/index.html">demo</a>:</p>
<p><a href="https://github.com/robotlegs/robotlegs-demos-Bundle/blob/v1.1.2/HelloFlex/src/org/robotlegs/demos/helloflex/HelloFlexContext.as#L30">https://github.com/robotlegs/robotlegs-demos-Bundle/blob/v1.1.2/Hel...</a><br />
<a href="https://github.com/robotlegs/robotlegs-demos-Bundle/blob/v1.1.2/HelloFlex/src/org/robotlegs/demos/helloflex/view/DemoCanvas.mxml#L19">https://github.com/robotlegs/robotlegs-demos-Bundle/blob/v1.1.2/Hel...</a></p></div>Shaun Smithtag:robotlegs.tenderapp.com,2009-10-18:Comment/25714952010-11-04T15:05:41Z2010-11-04T15:05:44ZMediator for viewstack<div><p>Shaun,</p>
<p>Thanks for pointing this out, I haven't looked at the demo for months.</p>
<p>The key difference between the demo and what I am asking is that my viewstack children have mediators whereas the demo's viewstack children (i.e. DemoCanvas) do not have mediators.</p>
<p>If the demo were to match my app, then DemoCanvas would have a mediator. This is where errors are being thrown because if <em>creationPolicy</em> is not set to <em>'all'</em> on the viewstack, the DemoCanvas is <em>null</em> when the mediator is created and is running onRegister().</p>
<p>For RobotLegs to work properly, should all viewstack (TabNavigator, Accordion, etc) children be wrapped in a canvas?</p></div>Brady Whitetag:robotlegs.tenderapp.com,2009-10-18:Comment/25714952010-11-04T15:29:43Z2010-11-04T15:29:43ZMediator for viewstack<div><p>If I don't set the creation policy to all I always get the null object error...<br />
Shaun is it possible to make a RL demo with viewstack items mediated?</p></div>christomanostag:robotlegs.tenderapp.com,2009-10-18:Comment/25714952010-11-04T18:21:45Z2010-11-04T18:21:45ZMediator for viewstack<div><p>Did you look at the actual lines that I linked to?</p></div>Shaun Smithtag:robotlegs.tenderapp.com,2009-10-18:Comment/25714952010-11-04T18:40:35Z2010-11-04T18:40:35ZMediator for viewstack<div><p>Yes, the accordion and its childs are already created which is not the same with the viewstack's childs if creation policy is not all</p></div>christomanostag:robotlegs.tenderapp.com,2009-10-18:Comment/25714952010-11-04T18:47:36Z2010-11-04T18:47:36ZMediator for viewstack<div><blockquote><p>the accordion and its childs are already created</p></blockquote>
<p>What makes you say that?</p></div>Shaun Smithtag:robotlegs.tenderapp.com,2009-10-18:Comment/25714952010-11-04T18:52:12Z2010-11-04T18:52:12ZMediator for viewstack<div><p>The purpose of that part of the demo was to show deferred mediation in action.</p></div>Shaun Smithtag:robotlegs.tenderapp.com,2009-10-18:Comment/25714952010-11-05T12:45:03Z2010-11-05T12:45:03ZMediator for viewstack<div><p>Apologies if I came across a bit rude - very tired from traveling/moving to a new country. The bottom line is this: RL mediators are explicitly designed to work <em>with</em> deferred instantiation - map a mediator class to a view class and an instance of that mediator will be created just-in-time when its corresponding view component lands on stage. The whole point of that work-flow is to ease development when dealing with things like ViewStacks, TabNavigators, Accordions etc, where children don't exist immediately.. hence the class (as opposed to instance) mapping thing.</p>
<p>You will only hit null-pointer errors if you try to manipulate children directly from their containers (parent) before they are ready. But you don't need to do that with RL's mediator implementation - the child mediator's onRegister hook will only fire when that view component is ready.</p>
<p>Here's a ZIP with a ViewStack demo. Hopefully it will clear things up a bit. Cheers</p></div>Shaun Smithtag:robotlegs.tenderapp.com,2009-10-18:Comment/25714952010-11-09T15:17:01Z2010-11-09T15:17:01ZMediator for viewstack<div><p>OK this is working perfect. I am trying now to see why my implementation fails...</p></div>christomanos