tag:robotlegs.tenderapp.com,2009-10-18:/discussions/questions/84-killing-viewsRobotlegs: Discussion 2018-10-18T16:35:09Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/11552212010-03-04T23:30:52Z2010-03-04T23:30:52ZKilling Views<div><pre>
// In a view
public function remove():void
{
// This would be for a plain ol' DisplayObject view
if ( parent.contains(this) ) parent.removeChild(this);
}
</pre>
<pre>
// In a mediator
view.remove();
</pre>
<p>That way the view can remove itself, or be told to do so from
the mediator (which in turn could be listening to the context's
event dispatcher).</p></div>Shaun Smithtag:robotlegs.tenderapp.com,2009-10-18:Comment/11552212010-03-04T23:34:29Z2010-03-04T23:34:37ZKilling Views<div><p>At that point won't it still be sitting in memory due to it
being created in the command. It is more of a resources issue than
just the removal from the display list.</p></div>embtag:robotlegs.tenderapp.com,2009-10-18:Comment/11552212010-03-04T23:52:01Z2010-03-04T23:52:01ZKilling Views<div><p>Nope, commands are short-lived objects - after execution nothing
holds onto them, so they become free for GC (along with their
references: properties and local variables alike).</p>
<p>Ideally, nothing other than a mediator should have a reference
to it's view. By default Robotlegs will automatically release the
mediator when it's view component leaves the stage. You can hook
into the mediator's onRemove() method to do any additional cleanup
before the entire system lets go of it.</p></div>Shaun Smithtag:robotlegs.tenderapp.com,2009-10-18:Comment/11552212010-03-05T00:12:12Z2010-03-05T00:12:14ZKilling Views<div><p>That is beyond fantastic! I am absolutely in love with
robotlegs.</p>
<p>Thanks so much for your help!</p></div>embtag:robotlegs.tenderapp.com,2009-10-18:Comment/11552212010-03-05T00:15:54Z2010-03-05T00:15:54ZKilling Views<div><p>Hey, no problem, glad to be of assistance :)</p></div>Shaun Smithtag:robotlegs.tenderapp.com,2009-10-18:Comment/11552212010-03-05T09:58:33Z2010-03-05T09:58:33ZKilling Views<div><p>I've heard rumours that the RL team actually wear their pants on
the outside...</p></div>Straytag:robotlegs.tenderapp.com,2009-10-18:Comment/11552212010-03-05T13:03:06Z2010-03-05T13:03:07ZKilling Views<div><p>Just to jump in on this discussion</p>
<p>There is one small hiccup with hooking into the Mediator's
onRemove() method and thats where your View registers an
eventListener on the stage.</p>
<p>By the time your view's dispose() method is called; it has
already been removed from the DisplayList and therefore won't be
able to remove the eventListener which references the stage.</p>
<p>Possible ways to get around this would be to use a weak
reference for the stage listener (a bit icky imho) or to override
the preRemove() method in the Mediator and call view.dispose()
there instead (again; I doubt the RL chaps would really want you
doing this).</p>
<p>any thoughts?</p></div>Jonny Reevestag:robotlegs.tenderapp.com,2009-10-18:Comment/11552212010-03-05T13:39:23Z2010-03-05T13:39:23ZKilling Views<div><p>If the stage listener is registered by the view internally, then
you can do that kind of cleanup in the view itself:</p>
<pre>
// In a view
public function remove():void
{
if ( parent.contains(this) )
{
// remove stage listener here
// ..
parent.removeChild(this);
}
}
</pre>
<p>If the listener is set up in the mediator via the eventMap, then
you don't need to worry about it as the eventMap cleans up all
listeners when the mediator is removed.</p></div>Shaun Smithtag:robotlegs.tenderapp.com,2009-10-18:Comment/11552212010-03-13T20:18:52Z2010-03-13T20:18:55ZKilling Views<div><p>Will the a Mediator instance be disposed if you have event
listeners attached to function inside the mediator and you don't
remove the listener?</p>
<p>e.g. You inject your view, you <code>addEventListener</code> on
the view inside <code>onRegister</code> and the handler function is
inside the mediator. Later the view is removed from stage, mediator
calls <code>onRemove</code>, but you don't remove the listener.</p>
<p>And does the <code>eventMap</code> automatically unmap the
listeners mapped on disposal of the mediator? Or do you have to
unmap it manually?</p></div>Matan Ubersteintag:robotlegs.tenderapp.com,2009-10-18:Comment/11552212010-03-13T20:20:47Z2010-03-13T20:20:47ZKilling Views<div><p>If you use the eventMap the listeners will be unregistered. If
you use addEventListener you will need to override onRemove and
remove the listeners manually.</p></div>Joel Hookstag:robotlegs.tenderapp.com,2009-10-18:Comment/11552212010-03-13T20:24:49Z2010-03-13T20:24:50ZKilling Views<div><p>Hey Joel,</p>
<p>I though so, can I:</p>
<p><code>eventMap.mapListener(view, SomeEvent.TYPE, handler,
SomeEvent);</code></p>
<p>and the listener will be removed automatically?</p></div>Matan Ubersteintag:robotlegs.tenderapp.com,2009-10-18:Comment/11552212010-03-13T20:26:06Z2010-03-13T20:26:06ZKilling Views<div><p><a href=
"http://github.com/robotlegs/robotlegs-framework/blob/master/src/org/robotlegs/mvcs/Mediator.as#L47">
http://github.com/robotlegs/robotlegs-framework/blob/master/src/org...</a></p>
<p>Yup</p></div>Joel Hookstag:robotlegs.tenderapp.com,2009-10-18:Comment/11552212010-03-13T20:30:04Z2010-03-13T20:30:05ZKilling Views<div><p>Awesome, it never occurred to me until now, now I have about a
100 places to go and modify code... But! It's for the best, better
that going everywhere and overriding onRemove and copying my view's
listener statements and changing them to remove.</p>
<p>Thanks,<br>
Joel</p></div>Matan Ubersteintag:robotlegs.tenderapp.com,2009-10-18:Comment/11552212010-03-15T14:46:37Z2010-03-15T14:46:42ZKilling Views<div><p>I know common knowledge to most, but from my experience with
projects such as kiosk apps that will be running continuously,
always better to avoid removing objects from the display list
unless necessary.<br>
better to simply set visibility to false.<br></p></div>eco_bachtag:robotlegs.tenderapp.com,2009-10-18:Comment/11552212010-03-15T14:52:09Z2010-03-15T14:52:09ZKilling Views<div><p>I like giving the system as much free memory as possible, it's
essential when building games or websites.</p></div>Matan Ubersteintag:robotlegs.tenderapp.com,2009-10-18:Comment/11552212010-03-15T15:02:31Z2010-03-15T15:02:36ZKilling Views<div><p>You will also see issues if the kiosk is being built on a low
end system like celeron or atom. If you have a beast of a box
running it, you'd prob be fine but otherwise you've got to kill
those views! =P</p></div>emb