Mediator doesn't destroy in Flex ViewStack
I have 2 views "view1" and "view2" in a viewstack,they implement the same interface.The mediator's destroy() does not run When I changed the view from "view1" to "view2" by settting the ViewStack's selectedIndex.
It seems like "view1" is still on the stage,the viewstack just changes it from visible to invisible.please tell me if this is the exact reason,thanks.
Comments are currently closed for this discussion. You can start a new one.
Keyboard shortcuts
Generic
? | Show this help |
---|---|
ESC | Blurs the current field |
Comment Form
r | Focus the comment reply box |
---|---|
^ + ↩ | Submit the comment |
You can use Command ⌘
instead of Control ^
on Mac
Support Staff 1 Posted by Ondina D.F. on Nov 28, 2013 @ 09:19 AM
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.
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.
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.
and inside your mediators something like this:
ViewOneMediator
ViewTwoMediator
More about guards:
https://github.com/robotlegs/robotlegs-framework/blob/master/src/ro...
Let me know if that helped.
Ondina
Support Staff 2 Posted by creynders on Nov 28, 2013 @ 09:24 AM
Ah, very cool example of using guards!
3 Posted by airycanon on Nov 29, 2013 @ 01:51 AM
Thanks for you nice sample.
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.
Support Staff 4 Posted by Ondina D.F. on Nov 29, 2013 @ 09:15 AM
Well, if you want to manually mediate/unmediate your views you can achieve this by using :
mediatorMap.unmediate(item:Object)
mediatorMap.mediate(item:Object)
One possible solution would be to mediate the view holding your viewstack.
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:
mediatorMap.unmediate(event.payload.hiddenView)
mediatorMap.mediate(event.payload.visibleView)
Not sure whether this is what you wanted..
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:)
@creynders thanks:)
5 Posted by airycanon on Dec 02, 2013 @ 01:23 AM
It‘s very kind of you,thanks for your help,and thanks for this wonderful framework.
airycanon closed this discussion on Dec 02, 2013 @ 01:23 AM.