home
  • Blog
1.12
  • Getting Started
  • Concepts
  • The Object Model
  • Application
  • Templates
  • Routing
  • Components
  • Controllers
  • Models
  • Views
    • Introduction
    • Defining a View
    • Handling Events
    • Inserting Views in Templates
    • Adding Layouts to Views
    • Customizing a View's Element
    • Built-in Views
    • Manually Managing View Hierarchy
  • Enumerables
  • Testing
  • Configuring Ember.js
  • Ember Inspector
  • Cookbook
  • Understanding Ember.js
  • Contributing to Ember.js
Old Guides - You are viewing the guides for Ember v1.12.0.
Go to v5.0.0

Manually Managing View Hierarchy

Edit pencil

Ember.ContainerView

As you probably know by now, views usually create their child views by using the {{view}} helper. However, it is sometimes useful to manually manage a view's child views. Ember.ContainerView is the way to do just that.

As you programmatically add or remove views to a ContainerView, those views' rendered HTML are added or removed from the DOM to match.

var container = Ember.ContainerView.create();
container.append();

var firstView = App.FirstView.create(),
    secondView = App.SecondView.create();

container.pushObject(firstView);
container.pushObject(secondView);

// When the rendering completes, the DOM
// will contain a `div` for the ContainerView
// and nested inside of it, a `div` for each of
// firstView and secondView.

Defining the Initial Views of a Container View

There are a few ways to specify which initial child views a ContainerView should render. The most straight-forward way is to add them in init:

var container = Ember.ContainerView.create({
  init: function() {
    this._super();
    this.pushObject(App.FirstView.create());
    this.pushObject(App.SecondView.create());
  }
});

container.objectAt(0).toString(); //=> '<App.FirstView:ember123>'
container.objectAt(1).toString(); //=> '<App.SecondView:ember124>'

As a shorthand, you can specify a childViews property that will be consulted on instantiation of the ContainerView also. This example is equivalent to the one above:

var container = Ember.ContainerView.extend({
  childViews: [App.FirstView, App.SecondView]
});

container.objectAt(0).toString(); //=> '<App.FirstView:ember123>'
container.objectAt(1).toString(); //=> '<App.SecondView:ember124>'

Another bit of syntactic sugar is available as an option as well: specifying string names in the childViews property that correspond to properties on the ContainerView. This style is less intuitive at first but has the added bonus that each named property will be updated to reference its instantiated child view:

var container = Ember.ContainerView.create({
  childViews: ['firstView', 'secondView'],
  firstView: App.FirstView,
  secondView: App.SecondView
});

container.objectAt(0).toString(); //=> '<App.FirstView:ember123>'
container.objectAt(1).toString(); //=> '<App.SecondView:ember124>'

container.get('firstView').toString(); //=> '<App.FirstView:ember123>'
container.get('secondView').toString(); //=> '<App.SecondView:ember124>'

It Feels Like an Array Because it is an Array

You may have noticed that some of these examples use pushObject to add a child view, just like you would interact with an Ember array. Ember.ContainerView gains its collection-like behavior by mixing in Ember.MutableArray. That means that you can manipulate the collection of views very expressively, using methods like pushObject, popObject, shiftObject, unshiftObject, insertAt, removeAt, or any other method you would use to interact with an Ember array.

left arrow
Built-in Views
We've finished covering Views. Next up: Enumerables - Introduction
right arrow
On this page

  • Ember.ContainerView
  • Defining the Initial Views of a Container View
  • It Feels Like an Array Because it is an Array
Team Sponsors Security Legal Branding Community Guidelines
Twitter GitHub Discord Mastodon

If you want help you can contact us by email, open an issue, or get realtime help by joining the Ember Discord.

© Copyright 2023 - Tilde Inc.
Ember.js is free, open source and always will be.


Ember is generously supported by
blue