Adding a View inside another View via a Command

Ryan Yacyshyn's Avatar

Ryan Yacyshyn

27 Jan, 2012 06:55 PM

Hi Everyone,

I'm working on a project and so far I've just been adding additional views as needed in a command class like so:

mediatorMap.mapView(NewView, NewViewMediator); contextView.addChild(new NewView());

Directly onto the contextView. This is working great; however, now I realize I also need to nest some (mediated) views inside another. For example, I want to have a main container that holds other views so that I can move the main container on the screen as needed. I want to add these new views inside this main container but I don't know how to access this main container through a command. Do I need to inject the main container view somehow into my command so that I can do something like:

mainContainer.addChild (new View());

I think it's risky to do something like:


Any thoughts would be much appreciated!


  1. Support Staff 1 Posted by creynders on 28 Jan, 2012 09:11 AM

    creynders's Avatar

    I would recommend against manipulating the display list by a command:

    • it gives the command too much knowledge about the display list hierarchy

    • it's view logic

    And IMO the mapping of views to mediators should happen in one or more view bootstrapping commands, because it centralizes the view mappings.

    In fact in all my applications the adding and removing of display objects is entirely the responsibility of the view tier itself. Display objects expose public methods that can be called by other display objects (either directly or through events) to add or remove other display objects, or if a display object is mediated the methods can be called by the corresponding mediator as well (in response to a system-wide event).
    This setup is extremely flexible, in my experience display objects easily change place in display list hierarchy and if the system is aware of their hierarchical place you'll need to make changes in-system every time.

  2. 2 Posted by Ryan Yacyshyn on 28 Jan, 2012 06:08 PM

    Ryan Yacyshyn's Avatar

    Ok that makes sense.

    I can certainly restructure things around so that my new views are created within another view rather than in a command. It would be easy then to add this view inside others.

    I have a startup command that runs once when my app is started, is this a good place then to map all my views to meditators?

  3. Support Staff 3 Posted by creynders on 29 Jan, 2012 06:59 PM

    creynders's Avatar

    Well, yes and no. :)
    With very small applications you can get away with putting all bootstrapping code in one command, but it quickly becomes huge and gives problems when the bootstrapping requires the addition of some kind of async process.
    The common way is to map 3 separate commands to the STARTUP event :

    Personally I add 2 more:

    And if it's a really big application I have all 5 commands for each functional "unit".

    Here's an example of a more complex bootstrapping process:

  4. Ondina D.F. closed this discussion on 23 Feb, 2012 10:35 AM.

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