how to add and remove views and their mediators

mike's Avatar

mike

14 Jun, 2011 06:35 PM

HI All
I hope you can help me I have views with their corresponding mediators and at the moment I attach them all to the stage. But because this is an adobe air for iOs project I just want to add the views on when i select their section and remove it when I go to another section. I just would like to know how to add the view and mediator and remove the view and mediators? Or is there a way to remove views and leave mediators to add a new view maybe.

regards Mike :)

  1. 1 Posted by Stray on 14 Jun, 2011 07:24 PM

    Stray's Avatar

    Hi Mike,

    when you add a view, the mediatorMap will create the mediator automatically.

    When you remove that view, the mediator will then be destroyed automatically.

    You just add the views and remove them in your normal way. ie addChild / removeChild in a parent view that is on the stage already.

    All you have to do is make sure that in your context you've told the mediatorMap how you'd like to mediate this view - with code like this:

    mediatorMap.mapView(SomeView, SomeViewMediator);

    One more tip: always wire your mediators from the most deeply nested down to the least deeply nested.

    And also, you can mediate your main / root view.

    I hope that helps,

    Stray

  2. 2 Posted by mike on 14 Jun, 2011 08:02 PM

    mike's Avatar

    Hi Stray
    Thanks for the answer. So one more question I have all my views mediated like below:

    injector.mapSingleton( SomeViewMediator);

    mediatorMap.mapView(SomeView, SomeViewMediator);

    So from what you are saying it is alright to remove and add views. Now I am guessing in order to remove the views I would do:

    contextView.removeChild(SomeView);

    and to add the view:

    contextView.addChild(SomeView);

    Am I correct?

    regards Mike :)

  3. 3 Posted by Stray on 14 Jun, 2011 08:09 PM

    Stray's Avatar

    Hi Mike, you shouldn't map your mediators as Singletons.

    Singleton mappings are only for models and services. That mapping should be totally unnecessary - just delete it, it's not the same instance of the mediator that will be used for the view anyway.

    Yes - you'd add / remove as you've put, but usually not in your context - you'd do that adding / removing inside the context view itself. If you need to trigger it from commands or mediators or the like then you should access that through a public function on the contextView really - eg a function like "addMenu" or "hideMenu".

    It would be worth checking out some examples I think - it sounds like you're a little unclear how to get started? Have a wade through the thread called "150+ resources... etc ... " that is in the 'examples' forum - lots of great demos in there that will make it a bit clearer for you.

    Stray

  4. 4 Posted by mike on 14 Jun, 2011 08:50 PM

    mike's Avatar

    Hi Stray
    Sorry about this can you point me to an example which does what i need to do above it would make it a lot easier for me to see what is happening. Thank you for your help.

    regards Mike :)

  5. 5 Posted by thomas.thorsten... on 14 Jun, 2011 09:34 PM

    thomas.thorstensson's Avatar

    Hi Mike,

    You can map view without the singleton bit just like so and youre already doing it. I usually have a command:

    public class PrepViewCommand extends Command {
    
        /**
         * When a view component is added to stage
         *the Mediator is created.
         */
        override public function execute():void {
            mediatorMap.mapView(NavigationView, NavigationMediator);
            //We are also mediating the root view (contextView)
            mediatorMap.mapView(Application, ApplicationMediator);
            mediatorMap.createMediator(contextView);
        }
    }
    

    Above I am in the second case also mediating the main root view. So for that one, since the view already is on stage the mediator needs to be created "manually" using createMediator. But for other case just like in your own code just do mediatorMap.mapView(yourView,yourMediator) thats it.

    I then have in my ApplicationMediator (which mediates the root view):

    override public function onRegister():void {
            contextView.addChild(new VRView());
            contextView.addChild(new NavigationView());
        }
    

    and so forth. It is also there that I would remove views if need be...I got to grips with this by reading best practices at first when I started doing Robotlegs its really a good read methinks!

    Tata

  6. 6 Posted by mike on 14 Jun, 2011 10:23 PM

    mike's Avatar

    Hi Thomas
    So basically you have a view which adds a and removes your views instead of the context view handling that task. Am I correct?

    regards Mike :)

  7. 7 Posted by thomas.thorsten... on 14 Jun, 2011 11:10 PM

    thomas.thorstensson's Avatar

    Yup

    You could have addChild for this and that view in your ApplicationContext but it's not that tidy perhaps ?

    But you don't need to create a new View class. contextView is your default root view and provided by the framework.

    You just need to create a class called ApplicationMediator which is the same looking as any Mediator.

    And then write:

    mediatorMap.mapView(Application, ApplicationMediator);
    mediatorMap.createMediator(contextView);
    

    You do not need to create a class called Application or contextView as that is part of framework; just write as above and provided you created a class named ApplicationMediator, you then have just registered it to mediate contextView (root view in RL framework).

    And remember context view is already on stage so the above will execute the onRegister of the ApplicationMediator. (Whereas for a regular Mediator you would just use the mapView(viewName,mediatorName) and onRegister would execute when view is then later added to stage)

    So in your ApplicationMediator you can acess contextView cause its provided by framework ;and its in ApplicationMediator you add or remove views, upon contextView

     public class ApplicationMediator extends Mediator
    {
        public function ApplicationMediator()
        {
        }
    
        /**
         * Add views to the contextView API:
         */
        override public function onRegister():void {
            contextView.addChild(new NavigationView());
        }
    }
    
  8. 8 Posted by mike on 14 Jun, 2011 11:25 PM

    mike's Avatar

    Hi Thomas
    Thanks for the info will implement.
    regards Mike :)

  9. 9 Posted by thomas.thorsten... on 14 Jun, 2011 11:36 PM

    thomas.thorstensson's Avatar

    Thanks Mike :)

  10. Ondina D.F. closed this discussion on 01 Nov, 2011 11:26 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