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.