tag:robotlegs.tenderapp.com,2009-10-18:/discussions/problems/5818-viewmediator-changes-after-windowresizeRobotlegs: Discussion 2014-09-01T12:30:07Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/340257392014-08-05T14:44:35Z2014-08-05T14:44:35ZViewMediator changes after windowresize<div><p>Hello,</p>
<ul>
<li>Firstly, it's not a robotlegs bug. The mediator map provides
mediator creation for mapped views landing on the stage.</li>
</ul>
<blockquote>
<p>RobotLegs thinks that the whole view is added to the stage and a
new mediator is assigned</p>
</blockquote>
<p>For a fact, the view is first removed from stage and then added
to the stage again! That's what Adobe calls 'reparenting':</p>
<p>"A view state can change the parent container of a component.
Changing the parent container of a component is called reparenting
the component."</p>
<p>More details here:<br>
<a href=
"http://help.adobe.com/en_US/flex/using/WS2db454920e96a9e51e63e3d11c0bf63611-7ffa.html">
http://help.adobe.com/en_US/flex/using/WS2db454920e96a9e51e63e3d11c...</a></p>
<p>I don't recall exactly whether re-parenting after resizing was a
Flex bug or not, but if it was a bug it seems that it hasn't been
resolved. However, re-parenting of components happens under other
circumstances as well: drag and drop, transition effects..</p>
<ul>
<li>
<p>Secondly, Amy is absolutely right! Mediators shouldn't hold
state. That's what Models are for. Maybe it's time to refactor your
Mediators? :)</p>
</li>
<li>
<p>Anyway, a workaround could be this:</p>
</li>
</ul>
<pre>
<code>mediatorMap.map(SomeView)
.toMediator(SomeMediator)
.autoRemove(false)
.withGuards(SomeMediator);</code>
</pre>
<p>You set the autoRemove of the mediator to false, in order to
keep it alive when the view gets reparented. Of course, you'll have
to remove the mediator manually, if need be.</p>
<p>Then, you create a guard. For the sake of an example I created
it inside of SomeMediator. You can use another class, if you
wish.</p>
<pre>
<code>public function approve():Boolean
{
if( view.needsMediator )
return true;
else
return false;
}</code>
</pre>
<p>Inside your view you create getters and setters for a
needsMediator and set it to true.<br>
Then, you set the view.needsMediator = false within mediator's
initialize().<br>
The first time SomeMediator is about to be created,
view.needsMediator is true, so the guard will 'approve' the
creation of the mediator.<br>
Next time, after reparenting the view, view.needsMediator will
return false, thus the guard won't approve the creation of a new
mediator for SomeView. The old mediator will react to events coming
from SomeView or framework classes.</p>
<p>needsMediator is a silly name. It's up to you how you name the
flag, and whether it is a property of the view or of the
mediator.</p>
<p>You'll have to try this out with your settings to see if it
works for you. I can't reproduce your use case on my end, thus I
can't guarantee that my workaround will solve your problem.</p>
<p>Let us know about your progress.</p>
<p>Ondina</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/340257392014-09-01T12:30:05Z2014-09-01T12:30:05ZViewMediator changes after windowresize<div><p>I'm assuming you've resolved the issue, thus I'm going to close
this discussion. Feel free to re-open it, if you want to respond or
if you need more assistance with this.</p></div>Ondina D.F.