Ember.View Class
DEPRECATED
PUBLIC
Extends: Ember.CoreView
Uses: Ember.ViewSupport
Uses: Ember.ViewChildViewsSupport
Uses: Ember.ClassNamesSupport
Uses: Ember.AttributeBindingsSupport
Uses: Ember.InstrumentationSupport
Uses: Ember.VisibilitySupport
Uses: Ember.AriaRoleSupport
Defined in: packages/ember-views/lib/views/view.js:17
Module: ember-views
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:
ParagraphView = Ember.View.extend({ tagName: 'em' });
Would result in instances with the following HTML:
<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:
MyView = Ember.View.extend({ classNames: ['my-class', 'my-other-class'] });
Will result in view instances with an HTML representation of:
<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:
MyView = Ember.View.extend({ classNameBindings: ['propertyA', 'propertyB'], propertyA: 'from-a', propertyB: Ember.computed(function() { if (someLogic) { return 'from-b'; } }) });
Will result in view instances with an HTML representation of:
<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
.
MyView = Ember.View.extend({ classNameBindings: ['hovered'], hovered: true });
Will result in view instances with an HTML representation of:
<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:
MyView = Ember.View.extend({ classNameBindings: ['awesome:so-very-cool'], awesome: true });
Will result in view instances with an HTML representation of:
<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:
MyView = Ember.View.extend({ classNameBindings: ['isUrgent'], isUrgent: true });
Will result in view instances with an HTML representation of:
<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:
MyView = Ember.View.extend({ classNameBindings: ['messages.empty'] messages: Ember.Object.create({ empty: true }) });
Will result in view instances with an HTML representation of:
<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:
// 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:
<div id="ember1" class="ember-view enabled"></div>
When isEnabled is false
, the resulting HTML representation looks like this:
<div id="ember1" class="ember-view disabled"></div>
This syntax offers the convenience to add a class if a property is false
:
// 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:
<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:
<div id="ember1" class="ember-view disabled"></div>
Updates to 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:
AnchorView = Ember.View.extend({ tagName: 'a', attributeBindings: ['href'], href: 'http://google.com' });
Will result in view instances with an HTML representation of:
<a id="ember1" class="ember-view" href="http://google.com"></a>
One property can be mapped on to another by placing a ":" between the source property and the destination property:
AnchorView = Ember.View.extend({ tagName: 'a', attributeBindings: ['url:href'], url: 'http://google.com' });
Will result in view instances with an HTML representation of:
<a id="ember1" class="ember-view" href="http://google.com"></a>
Namespaced attributes (e.g. xlink:href
) are supported, but have to be mapped, since :
is not a valid character for properties in Javascript:
UseView = Ember.View.extend({ tagName: 'use', attributeBindings: ['xlinkHref:xlink:href'], xlinkHref: '#triangle' });
Will result in view instances with an HTML representation of:
<use xlink:href="#triangle"></use>
If the return value of an attributeBindings
monitored property is a boolean the attribute will be present or absent depending on the value:
MyTextInput = Ember.View.extend({ tagName: 'input', attributeBindings: ['disabled'], disabled: false });
Will result in a view instance with an HTML representation of:
<input id="ember1" class="ember-view" />
attributeBindings
can refer to computed properties:
MyTextInput = Ember.View.extend({ tagName: 'input', attributeBindings: ['disabled'], disabled: Ember.computed(function() { if (someLogic) { return true; } else { return false; } }) });
To prevent setting an attribute altogether, use null
or undefined
as the return value of the attributeBindings
monitored property:
MyTextInput = Ember.View.extend({ tagName: 'form', attributeBindings: ['novalidate'], novalidate: null });
Updates to 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.
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 template.
A view's layout can be set directly with the layout
property or reference an existing template by name with the layoutName
property.
A template used as a layout must contain a single use of the {{yield}}
helper. The HTML contents of a view's rendered template
will be inserted at this location:
AViewWithLayout = Ember.View.extend({ layout: Ember.HTMLBars.compile("<div class='my-decorative-class'>{{yield}}</div>"), template: Ember.HTMLBars.compile("I got wrapped") });
Will result in view instances with an HTML representation of:
<div id="ember1" class="ember-view"> <div class="my-decorative-class"> I got wrapped </div> </div>
See Ember.Templates.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.
AView = Ember.View.extend({ click: function(event) { // will be called 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 descendant views.
AView = Ember.View.extend({ eventManager: Ember.Object.create({ doubleClick: function(event, view) { // will be called when an instance's // rendered element or any rendering // of this view's descendant // elements is clicked } }) });
An event defined for an event manager takes precedence over events of the same name handled through methods on the view.
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 descendant. 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 descendant.
var App = Ember.Application.create(); App.OuterView = Ember.View.extend({ template: Ember.HTMLBars.compile("outer {{#view 'inner'}}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 occurred } }) }); App.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. } });
{{action}}
Helper
See Ember.Templates.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
dragOver
dragEnd
drop
Please login to continue.