How do I keep my view from getting extra mediators every time it is added to the stage?
Support Staff2 Posted by Shaun Smith on 20 Apr, 2010 08:30 PM
Fully automatic mediation ensures that mediators are created AND
removed as view components are added to and removed from the
display list. Each time a mapped view component is added to stage a
mediator is created for it; and when it leaves, the corresponding
mediator is removed.
By default, a Mediator in Robotlegs acts as a stateless bridge
between an on-screen view component and the framework. When a view
component is not on-screen it is not mediated. Is there much point
in updating an off-screen view component? Not usually.
That said, there are times when the default behavior is not
desired, in which case you can decide which parts you'd like
automated, and which parts you will be responsible for yourself.
You can control this behavior with the 3rd and 4th parameters of
* Map an IMediator to a view Class
* @param viewClassOrName The concrete view Class or Fully Qualified Class Name
* @param mediatorClass The IMediator Class
* @param injectViewAs The explicit view Interface or Class that the mediator depends on
* @param autoCreate Automatically construct and register an instance of Class mediatorClass when an instance of Class viewClass is detected
* @param autoRemove Automatically remove an instance of Class mediatorClass when it's viewClass leaves the ancestory of the context view
function mapView(viewClassOrName:*, mediatorClass:Class, injectViewAs:Class = null, autoCreate:Boolean = true, autoRemove:Boolean = true):void;
If you want to manually mediate a view component, set both
autoCreate and autoRemove to false. You
will then be responsible for manually creating the appropriate
mediator by calling mediatorMap.createMediator(view);,
and removing it with
If you'd like mediators to be created automatically, but not to
be removed when the view leaves the stage, set
autoRemove to false, but be sure to remove the
mediator when you no longer need it.
I wanted to add that the reason the mediator is not being
removed is because you aren't cleaning up your listeners. If you
add an event listener, or a signal, to a view component then you
have to make sure to clean those up When you remove the component
or else the mediator will never actually be removed. I guess using
the eventMap to add listeners would get around this issue.
We don't depend on garbage collection for mediator removal - we unmap everything manually, and delete as much as possible, but - as with any code in any situation in flash - if you leave references scattered around (like your signal handler mapping) then they will persist and the object will continue to exist. It won't keep firing eventMap mapped events, because these have been unmapped directly.
It was *you* who was hoping that garbage collection would clean up your signal handler. The framework is fine.
I find that in a lot of cases I dont want to auto remove my
mediators, especially when I have a command that gets data that
refreshes a lot of my models, which the mediators then listen to
the changes and update the views. If the mediators get removed then
I find having to add checks in the meditors on register to see if
the data has changed or not to update the view.