Class Ember.View

Ember.View is the class in Ember responsible for encapsulating templates of HTML content, combining templates with data to render as sections of a page's DOM, and registering and responding to user-initiated events.

HTML Tag

The default HTML tag name used for a view's DOM representation is div. This can be customized by setting the tagName property. The following view class:

1
2
3
ParagraphView = Ember.View.extend({
  tagName: 'em'
});

Would result in instances with the following HTML:

1
<em id="ember1" class="ember-view"></em>

HTML class Attribute

The HTML class attribute of a view's tag can be set by providing a classNames property that is set to an array of strings:

1
2
3
MyView = Ember.View.extend({
  classNames: ['my-class', 'my-other-class']
});

Will result in view instances with an HTML representation of:

1
<div id="ember1" class="ember-view my-class my-other-class"></div>

class attribute values can also be set by providing a classNameBindings property set to an array of properties names for the view. The return value of these properties will be added as part of the value for the view's class attribute. These properties can be computed properties:

1
2
3
4
5
6
7
MyView = Ember.View.extend({
  classNameBindings: ['propertyA', 'propertyB'],
  propertyA: 'from-a',
  propertyB: function() {
    if (someLogic) { return 'from-b'; }
  }.property()
});

Will result in view instances with an HTML representation of:

1
<div id="ember1" class="ember-view from-a from-b"></div>

If the value of a class name binding returns a boolean the property name itself will be used as the class name if the property is true. The class name will not be added if the value is false or undefined.

1
2
3
4
MyView = Ember.View.extend({
  classNameBindings: ['hovered'],
  hovered: true
});

Will result in view instances with an HTML representation of:

1
<div id="ember1" class="ember-view hovered"></div>

When using boolean class name bindings you can supply a string value other than the property name for use as the class HTML attribute by appending the preferred value after a ":" character when defining the binding:

1
2
3
4
MyView = Ember.View.extend({
  classNameBindings: ['awesome:so-very-cool'],
  awesome: true
});

Will result in view instances with an HTML representation of:

1
<div id="ember1" class="ember-view so-very-cool"></div>

Boolean value class name bindings whose property names are in a camelCase-style format will be converted to a dasherized format:

1
2
3
4
MyView = Ember.View.extend({
  classNameBindings: ['isUrgent'],
  isUrgent: true
});

Will result in view instances with an HTML representation of:

1
<div id="ember1" class="ember-view is-urgent"></div>

Class name bindings can also refer to object values that are found by traversing a path relative to the view itself:

1
2
3
4
5
6
MyView = Ember.View.extend({
  classNameBindings: ['messages.empty']
  messages: Ember.Object.create({
    empty: true
  })
});

Will result in view instances with an HTML representation of:

1
<div id="ember1" class="ember-view empty"></div>

If you want to add a class name for a property which evaluates to true and and a different class name if it evaluates to false, you can pass a binding like this:

1
2
3
4
5
// Applies 'enabled' class when isEnabled is true and 'disabled' when isEnabled is false
Ember.View.extend({
  classNameBindings: ['isEnabled:enabled:disabled']
  isEnabled: true
});

Will result in view instances with an HTML representation of:

1
<div id="ember1" class="ember-view enabled"></div>

When isEnabled is false, the resulting HTML reprensentation looks like this:

1
<div id="ember1" class="ember-view disabled"></div>

This syntax offers the convenience to add a class if a property is false:

1
2
3
4
5
// Applies no class when isEnabled is true and class 'disabled' when isEnabled is false
Ember.View.extend({
  classNameBindings: ['isEnabled::disabled']
  isEnabled: true
});

Will result in view instances with an HTML representation of:

1
<div id="ember1" class="ember-view"></div>

When the isEnabled property on the view is set to false, it will result in view instances with an HTML representation of:

1
<div id="ember1" class="ember-view disabled"></div>

Updates to the the value of a class name binding will result in automatic update of the HTML class attribute in the view's rendered HTML representation. If the value becomes false or undefined the class name will be removed.

Both classNames and classNameBindings are concatenated properties. See Ember.Object documentation for more information about concatenated properties.

HTML Attributes

The HTML attribute section of a view's tag can be set by providing an attributeBindings property set to an array of property names on the view. The return value of these properties will be used as the value of the view's HTML associated attribute:

1
2
3
4
5
AnchorView = Ember.View.extend({
  tagName: 'a',
  attributeBindings: ['href'],
  href: 'http://google.com'
});

Will result in view instances with an HTML representation of:

1
<a id="ember1" class="ember-view" href="http://google.com"></a>

If the return value of an attributeBindings monitored property is a boolean the property will follow HTML's pattern of repeating the attribute's name as its value:

1
2
3
4
5
MyTextInput = Ember.View.extend({
  tagName: 'input',
  attributeBindings: ['disabled'],
  disabled: true
});

Will result in view instances with an HTML representation of:

1
<input id="ember1" class="ember-view" disabled="disabled" />

attributeBindings can refer to computed properties:

1
2
3
4
5
6
7
8
9
10
11
MyTextInput = Ember.View.extend({
  tagName: 'input',
  attributeBindings: ['disabled'],
  disabled: function() {
    if (someLogic) {
      return true;
    } else {
      return false;
    }
  }.property()
});

Updates to the the property of an attribute binding will result in automatic update of the HTML attribute in the view's rendered HTML representation.

attributeBindings is a concatenated property. See Ember.Object documentation for more information about concatenated properties.

Templates

The HTML contents of a view's rendered representation are determined by its template. Templates can be any function that accepts an optional context parameter and returns a string of HTML that will be inserted within the view's tag. Most typically in Ember this function will be a compiled Ember.Handlebars template.

1
2
3
AView = Ember.View.extend({
  template: Ember.Handlebars.compile('I am the template')
});

Will result in view instances with an HTML representation of:

1
<div id="ember1" class="ember-view">I am the template</div>

Within an Ember application is more common to define a Handlebars templates as part of a page:

1
2
3
<script type='text/x-handlebars' data-template-name='some-template'>
  Hello
</script>

And associate it by name using a view's templateName property:

1
2
3
AView = Ember.View.extend({
  templateName: 'some-template'
});

Using a value for templateName that does not have a Handlebars template with a matching data-template-name attribute will throw an error.

For views classes that may have a template later defined (e.g. as the block portion of a {{view}} Handlebars helper call in another template or in a subclass), you can provide a defaultTemplate property set to compiled template function. If a template is not later provided for the view instance the defaultTemplate value will be used:

1
2
3
4
5
AView = Ember.View.extend({
  defaultTemplate: Ember.Handlebars.compile('I was the default'),
  template: null,
  templateName: null
});

Will result in instances with an HTML representation of:

1
<div id="ember1" class="ember-view">I was the default</div>

If a template or templateName is provided it will take precedence over defaultTemplate:

1
2
3
4
5
6
7
AView = Ember.View.extend({
  defaultTemplate: Ember.Handlebars.compile('I was the default')
});

aView = AView.create({
  template: Ember.Handlebars.compile('I was the template, not default')
});

Will result in the following HTML representation when rendered:

1
<div id="ember1" class="ember-view">I was the template, not default</div>

View Context

The default context of the compiled template is the view's controller:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
AView = Ember.View.extend({
  template: Ember.Handlebars.compile('Hello {{excitedGreeting}}')
});

aController = Ember.Object.create({
  firstName: 'Barry',
  excitedGreeting: function() {
    return this.get("content.firstName") + "!!!"
  }.property()
});

aView = AView.create({
  controller: aController,
});

Will result in an HTML representation of:

1
<div id="ember1" class="ember-view">Hello Barry!!!</div>

A context can also be explicitly supplied through the view's context property. If the view has neither context nor controller properties, the parentView's context will be used.

Layouts

Views can have a secondary template that wraps their main template. Like primary templates, layouts can be any function that accepts an optional context parameter and returns a string of HTML that will be inserted inside view's tag. Views whose HTML element is self closing (e.g. <input />) cannot have a layout and this property will be ignored.

Most typically in Ember a layout will be a compiled Ember.Handlebars template.

A view's layout can be set directly with the layout property or reference an existing Handlebars template by name with the layoutName property.

A template used as a layout must contain a single use of the Handlebars {{yield}} helper. The HTML contents of a view's rendered template will be inserted at this location:

1
2
3
4
AViewWithLayout = Ember.View.extend({
  layout: Ember.Handlebars.compile("<div class='my-decorative-class'>{{yield}}</div>")
  template: Ember.Handlebars.compile("I got wrapped"),
});

Will result in view instances with an HTML representation of:

1
2
3
4
5
<div id="ember1" class="ember-view">
  <div class="my-decorative-class">
    I got wrapped
  </div>
</div>

See Ember.Handlebars.helpers.yield for more information.

Responding to Browser Events

Views can respond to user-initiated events in one of three ways: method implementation, through an event manager, and through {{action}} helper use in their template or layout.

Method Implementation

Views can respond to user-initiated events by implementing a method that matches the event name. A jQuery.Event object will be passed as the argument to this method.

1
2
3
4
5
6
AView = Ember.View.extend({
  click: function(event) {
    // will be called when when an instance's
    // rendered element is clicked
  }
});

Event Managers

Views can define an object as their eventManager property. This object can then implement methods that match the desired event names. Matching events that occur on the view's rendered HTML or the rendered HTML of any of its DOM descendants will trigger this method. A jQuery.Event object will be passed as the first argument to the method and an Ember.View object as the second. The Ember.View will be the view whose rendered HTML was interacted with. This may be the view with the eventManager property or one of its descendent views.

1
2
3
4
5
6
7
8
9
10
AView = Ember.View.extend({
  eventManager: Ember.Object.create({
    doubleClick: function(event, view) {
      // will be called when when an instance's
      // rendered element or any rendering
      // of this views's descendent
      // elements is clicked
    }
  })
});

An event defined for an event manager takes precedence over events of the same name handled through methods on the view.

1
2
3
4
5
6
7
8
9
10
AView = Ember.View.extend({
  mouseEnter: function(event) {
    // will never trigger.
  },
  eventManager: Ember.Object.create({
    mouseEnter: function(event, view) {
      // takes precedence over AView#mouseEnter
    }
  })
});

Similarly a view's event manager will take precedence for events of any views rendered as a descendent. A method name that matches an event name will not be called if the view instance was rendered inside the HTML representation of a view that has an eventManager property defined that handles events of the name. Events not handled by the event manager will still trigger method calls on the descendent.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
OuterView = Ember.View.extend({
  template: Ember.Handlebars.compile("outer {{#view InnerView}}inner{{/view}} outer"),
  eventManager: Ember.Object.create({
    mouseEnter: function(event, view) {
      // view might be instance of either
      // OuterView or InnerView depending on
      // where on the page the user interaction occured
    }
  })
});

InnerView = Ember.View.extend({
  click: function(event) {
    // will be called if rendered inside
    // an OuterView because OuterView's
    // eventManager doesn't handle click events
  },
  mouseEnter: function(event) {
    // will never be called if rendered inside
    // an OuterView.
  }
});

Handlebars {{action}} Helper

See Handlebars.helpers.action.

Event Names

All of the event handling approaches described above respond to the same set of events. The names of the built-in events are listed below. (The hash of built-in events exists in Ember.EventDispatcher.) Additional, custom events can be registered by using Ember.Application.customEvents.

Touch events:

  • touchStart
  • touchMove
  • touchEnd
  • touchCancel

Keyboard events

  • keyDown
  • keyUp
  • keyPress

Mouse events

  • mouseDown
  • mouseUp
  • contextMenu
  • click
  • doubleClick
  • mouseMove
  • focusIn
  • focusOut
  • mouseEnter
  • mouseLeave

Form events:

  • submit
  • change
  • focusIn
  • focusOut
  • input

HTML5 drag and drop events:

  • dragStart
  • drag
  • dragEnter
  • dragLeave
  • drop
  • dragEnd

Handlebars {{view}} Helper

Other Ember.View instances can be included as part of a view's template by using the {{view}} Handlebars helper. See Ember.Handlebars.helpers.view for additional information.

Show:

Module: ember

The WAI-ARIA role of the control represented by this view. For example, a button may have a role of type 'button', or a pane may have a role of type 'alertdialog'. This property is used by assistive software to help visually challenged users navigate rich web applications.

The full list of valid WAI-ARIA roles is available at: http://www.w3.org/TR/wai-aria/roles#roles_categorization

Module: ember

A list of properties of the view to apply as attributes. If the property is a string value, the value of that string will be applied as the attribute.

1
2
3
4
5
6
// Applies the type attribute to the element
// with the value "button", like <div type="button">
Ember.View.extend({
  attributeBindings: ['type'],
  type: 'button'
});

If the value of the property is a Boolean, the name of that property is added as an attribute.

1
2
3
4
5
// Renders something like <div enabled="enabled">
Ember.View.extend({
  attributeBindings: ['enabled'],
  enabled: true
});
Module: ember

A list of properties of the view to apply as class names. If the property is a string value, the value of that string will be applied as a class name.

1
2
3
4
5
// Applies the 'high' class to the view element
Ember.View.extend({
  classNameBindings: ['priority']
  priority: 'high'
});

If the value of the property is a Boolean, the name of that property is added as a dasherized class name.

1
2
3
4
5
// Applies the 'is-urgent' class to the view element
Ember.View.extend({
  classNameBindings: ['isUrgent']
  isUrgent: true
});

If you would prefer to use a custom value instead of the dasherized property name, you can pass a binding like this:

1
2
3
4
5
// Applies the 'urgent' class to the view element
Ember.View.extend({
  classNameBindings: ['isUrgent:urgent']
  isUrgent: true
});

This list of properties is inherited from the view's superclasses as well.

Module: ember

Standard CSS class names to apply to the view's outer element. This property automatically inherits any class names defined by the view's superclasses as well.

Module: ember

Defines the properties that will be concatenated from the superclass (instead of overridden).

By default, when you extend an Ember class a property defined in the subclass overrides a property with the same name that is defined in the superclass. However, there are some cases where it is preferable to build up a property's value by combining the superclass' property value with the subclass' value. An example of this in use within Ember is the classNames property of Ember.View.

Here is some sample code showing the difference between a concatenated property and a normal one:

1
2
3
4
5
6
7
8
9
10
11
12
13
App.BarView = Ember.View.extend({
  someNonConcatenatedProperty: ['bar'],
  classNames: ['bar']
});

App.FooBarView = App.BarView.extend({
  someNonConcatenatedProperty: ['foo'],
  classNames: ['foo'],
});

var fooBarView = App.FooBarView.create();
fooBarView.get('someNonConcatenatedProperty'); // ['foo']
fooBarView.get('classNames'); // ['ember-view', 'bar', 'foo']

This behavior extends to object creation as well. Continuing the above example:

1
2
3
4
5
6
var view = App.FooBarView.create({
  someNonConcatenatedProperty: ['baz'],
  classNames: ['baz']
})
view.get('someNonConcatenatedProperty'); // ['baz']
view.get('classNames'); // ['ember-view', 'bar', 'foo', 'baz']

Adding a single property that is not an array will just add it in the array:

1
2
3
4
var view = App.FooBarView.create({
  classNames: 'baz'
})
view.get('classNames'); // ['ember-view', 'bar', 'foo', 'baz']

Using the concatenatedProperties property, we can tell to Ember that mix the content of the properties.

In Ember.View the classNameBindings and attributeBindings properties are also concatenated, in addition to classNames.

This feature is available for you to use throughout the Ember object model, although typical app developers are likely to use it infrequently.

Module: ember

The object from which templates should access properties.

This object will be passed to the template function each time the render method is called, but it is up to the individual function to decide what to do with it.

By default, this will be the view's controller.

Module: ember

The controller managing this view. If this property is set, it will be made available for use by the template.

Module: ember

Returns the current DOM element for the view.

Module: ember

Destroyed object property flag.

if this property is true the observers and bindings were already removed by the effect of calling the destroy() method.

Module: ember

Destruction scheduled flag. The destroy() method has been called.

The object stays intact until the end of the run loop at which point the isDestroyed flag is set.

Module: ember

concatenatedProperties: ['classNames', 'classNameBindings', 'attributeBindings'], /**

Module: ember

If false, the view will appear hidden in DOM.

Module: ember

A view may contain a layout. A layout is a regular template but supersedes the template property during rendering. It is the responsibility of the layout template to retrieve the template property from the view (or alternatively, call Handlebars.helpers.yield, {{yield}}) to render it in the correct location.

This is useful for a view that has a shared wrapper, but which delegates the rendering of the contents of the wrapper to the template property on a subclass.

Module: ember

The name of the layout to lookup if no layout is provided.

Ember.View will look for a template with this name in this view's templates object. By default, this will be a global object shared in Ember.TEMPLATES.

Module: ember
returns
Ember.View

Return the nearest ancestor whose parent is an instance of klass.

Module: ember
returns
Ember.View

Return the nearest ancestor that is an instance of the provided class or mixin.

Module: ember
returns
Ember.View

Return the nearest ancestor that has a given property.

Module: ember

If the view is currently inserted into the DOM of a parent view, this property will point to the parent of the view.

Module: ember

Tag name for the view's outer element. The tag name is only used when an element is first created. If you change the tagName for an element, you must destroy and recreate the view element.

By default, the render buffer will use a <div> tag for views.

Module: ember

The template used to render the view. This should be a function that accepts an optional context parameter and returns a string of HTML that will be inserted into the DOM relative to its parent view.

In general, you should set the templateName property instead of setting the template yourself.

Module: ember

The name of the template to lookup if no template is provided.

Ember.View will look for a template with this name in this view's templates object. By default, this will be a global object shared in Ember.TEMPLATES.

Module: ember

The hash in which to look for templateName.

Module: ember

Global views hash