tag:robotlegs.tenderapp.com,2009-10-18:/discussions/robotlegs-2/8397-mediator-doesnt-destroy-in-flex-viewstackRobotlegs: Discussion 2018-10-18T16:35:53Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/302453492013-11-28T09:19:24Z2013-11-28T09:19:24ZMediator doesn't destroy in Flex ViewStack<div><p>Yes, that's right, components of a ViewStack don't get removed
from stage when the index changes. So, when you select ViewTwo,
ViewOne still exists. It is just not visible. Thus, 2 mediators
will be created for each view.</p>
<p>There are workarounds for removing a container from a ViewStack
when another one is selected, but, I think, it's not worth the
effort. It would be easier to choose another way of navigation,
where you add and remove your views explicitly.<br>
If you want to keep your ViewStack though, you could add guards to
your mediators, which will decide if they have to be created or not
depending on some condition.</p>
<pre>
<code>mediatorMap.map(ISomeView).toMediator(ViewOneMediator).withGuards(ViewOneMediator);
mediatorMap.map(ISomeView).toMediator(ViewTwoMediator).withGuards(ViewTwoMediator);</code>
</pre>
<p>and inside your mediators something like this:</p>
<p>ViewOneMediator</p>
<pre>
<code>public function approve():Boolean
{
return view.viewName=="viewOne";
}</code>
</pre>
<p>ViewTwoMediator</p>
<pre>
<code>public function approve():Boolean
{
return view.viewName=="viewTwo";
}</code>
</pre>
<p>More 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>Let me know if that helped.<br>
Ondina</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/302453492013-11-28T09:24:47Z2013-11-28T09:24:47ZMediator doesn't destroy in Flex ViewStack<div><p>Ah, very cool example of using guards!</p></div>creynderstag:robotlegs.tenderapp.com,2009-10-18:Comment/302453492013-11-29T01:51:40Z2013-11-29T01:51:40ZMediator doesn't destroy in Flex ViewStack<div><p>Thanks for you nice sample.<br>
In my condition,both ViewOneMediator and ViewTwoMediator need to be
created when their views are selected in a ViewStack ,is there a
way to destroy the invisible view 's mediator?Because it has
unwanted eventListeners.</p></div>airycanontag:robotlegs.tenderapp.com,2009-10-18:Comment/302453492013-11-29T09:15:44Z2013-11-29T09:15:44ZMediator doesn't destroy in Flex ViewStack<div><p>Well, if you want to manually mediate/unmediate your views you
can achieve this by using :</p>
<p>mediatorMap.unmediate(item:Object)<br>
mediatorMap.mediate(item:Object)</p>
<p>One possible solution would be to mediate the view holding your
viewstack.<br>
In the view you add listeners for stack's IndexChangedEvent, get
the old and new indexes in order to get the hidden and the visible
views, and then dispatch an event with a payload containing the
old(hidden) child and the new(visible) child. The viewstack's
mediator listens for the view events and performs the manual
mediation/unmediation:</p>
<p>mediatorMap.unmediate(event.payload.hiddenView)<br>
mediatorMap.mediate(event.payload.visibleView)</p>
<p>Not sure whether this is what you wanted..</p>
<p>As I said in my previous post, maybe you should reconsider the
way you're designing the navigation, so that a view gets removed
from stage when you navigate to another, if you don't need/want it
to be kept alive. But, you are the one knowing your use case and
what's best for it:)</p>
<p>@creynders thanks:)</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/302453492013-12-02T01:23:33Z2013-12-02T01:23:33ZMediator doesn't destroy in Flex ViewStack<div><p>It‘s very kind of you,thanks for your help,and thanks for
this wonderful framework.</p></div>airycanon