Mediator doesn't destroy in Flex ViewStack

airycanon's Avatar

airycanon

Nov 28, 2013 @ 02:41 AM

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.

  1. Support Staff 1 Posted by Ondina D.F. on Nov 28, 2013 @ 09:19 AM

    Ondina D.F.'s Avatar

    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.

    mediatorMap.map(ISomeView).toMediator(ViewOneMediator).withGuards(ViewOneMediator);
    mediatorMap.map(ISomeView).toMediator(ViewTwoMediator).withGuards(ViewTwoMediator);
    

    and inside your mediators something like this:

    ViewOneMediator

    public function approve():Boolean
    {
        return view.viewName=="viewOne";
    }
    

    ViewTwoMediator

    public function approve():Boolean
    {
        return view.viewName=="viewTwo";
    }
    

    More about guards:
    https://github.com/robotlegs/robotlegs-framework/blob/master/src/ro...

    Let me know if that helped.
    Ondina

  2. Support Staff 2 Posted by creynders on Nov 28, 2013 @ 09:24 AM

    creynders's Avatar

    Ah, very cool example of using guards!

  3. 3 Posted by airycanon on Nov 29, 2013 @ 01:51 AM

    airycanon's Avatar

    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.

  4. Support Staff 4 Posted by Ondina D.F. on Nov 29, 2013 @ 09:15 AM

    Ondina D.F.'s Avatar

    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. 5 Posted by airycanon on Dec 02, 2013 @ 01:23 AM

    airycanon's Avatar

    It‘s very kind of you,thanks for your help,and thanks for this wonderful framework.

  6. airycanon closed this discussion on Dec 02, 2013 @ 01:23 AM.

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