Mediator for viewstack

christomanos's Avatar


13 Aug, 2010 07:14 PM

Hi all,

Maybe this is common knowledge but it took me 2 days to figure it out. If you want to map a viewstack's NavigatorContent you need to set the creationPolicy to all cause ViewStack children are created only when needed by default.

Hope this helps

  1. 1 Posted by Brady White on 03 Nov, 2010 04:59 PM

    Brady White's Avatar

    I'd love to see a best practice for how to manage mediators of viewstack children. Setting the viewstack's creationPolicy to all doesn't sound like the best solution as far as performance, but definitely works.

    I'm experimenting with the following.:

    Code on Parent Mediator of ViewStack

        override public function onRegister():void
            // Manually Mediate ViewStack
            eventMap.mapListener(view.viewstack, IndexChangedEvent.CHANGE, handleViewStackChange);
         * Manually Mediate ViewStack Children
        private function handleViewStackChange(e:IndexChangedEvent):void
            var oldView:Object = view.viewstack.getChildAt(e.oldIndex);
            mediatorMap.removeMediatorByView( oldView );
            mediatorMap.createMediator( view.viewstack.selectedChild  ); 

    The ApplicationContext has each child of the viewstack manually mediated as follows:
    mediatorMap.mapView( MyChildView, MyChildViewMediator, null, false, false );

    This currently is not working because the mediator is being created before the child view when the index changes. Thoughts on this?

  2. Support Staff 2 Posted by Shaun Smith on 04 Nov, 2010 02:22 AM

    Shaun Smith's Avatar


    Auto-mediation (the default) is designed to work with deferred instantiation. Setting "creationPolicy" to "all" and/or manually creating mediators on viewstack-index-change defeats the point. From the very first RL demo:

  3. 3 Posted by Brady White on 04 Nov, 2010 03:05 PM

    Brady White's Avatar


    Thanks for pointing this out, I haven't looked at the demo for months.

    The key difference between the demo and what I am asking is that my viewstack children have mediators whereas the demo's viewstack children (i.e. DemoCanvas) do not have mediators.

    If the demo were to match my app, then DemoCanvas would have a mediator. This is where errors are being thrown because if creationPolicy is not set to 'all' on the viewstack, the DemoCanvas is null when the mediator is created and is running onRegister().

    For RobotLegs to work properly, should all viewstack (TabNavigator, Accordion, etc) children be wrapped in a canvas?

  4. 4 Posted by christomanos on 04 Nov, 2010 03:29 PM

    christomanos's Avatar

    If I don't set the creation policy to all I always get the null object error...
    Shaun is it possible to make a RL demo with viewstack items mediated?

  5. Support Staff 5 Posted by Shaun Smith on 04 Nov, 2010 06:21 PM

    Shaun Smith's Avatar

    Did you look at the actual lines that I linked to?

  6. 6 Posted by christomanos on 04 Nov, 2010 06:40 PM

    christomanos's Avatar

    Yes, the accordion and its childs are already created which is not the same with the viewstack's childs if creation policy is not all

  7. Support Staff 7 Posted by Shaun Smith on 04 Nov, 2010 06:47 PM

    Shaun Smith's Avatar

    the accordion and its childs are already created

    What makes you say that?

  8. Support Staff 8 Posted by Shaun Smith on 04 Nov, 2010 06:52 PM

    Shaun Smith's Avatar

    The purpose of that part of the demo was to show deferred mediation in action.

  9. Support Staff 9 Posted by Shaun Smith on 05 Nov, 2010 12:45 PM

    Shaun Smith's Avatar

    Apologies if I came across a bit rude - very tired from traveling/moving to a new country. The bottom line is this: RL mediators are explicitly designed to work with deferred instantiation - map a mediator class to a view class and an instance of that mediator will be created just-in-time when its corresponding view component lands on stage. The whole point of that work-flow is to ease development when dealing with things like ViewStacks, TabNavigators, Accordions etc, where children don't exist immediately.. hence the class (as opposed to instance) mapping thing.

    You will only hit null-pointer errors if you try to manipulate children directly from their containers (parent) before they are ready. But you don't need to do that with RL's mediator implementation - the child mediator's onRegister hook will only fire when that view component is ready.

    Here's a ZIP with a ViewStack demo. Hopefully it will clear things up a bit. Cheers

  10. 10 Posted by christomanos on 09 Nov, 2010 03:17 PM

    christomanos's Avatar

    OK this is working perfect. I am trying now to see why my implementation fails...

  11. Stray closed this discussion on 13 Feb, 2011 04:54 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts


? 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