Class Component
publicimport Component from '@ember/component';
A Component
is a view that is completely
isolated. Properties accessed in its templates go
to the view object and actions are targeted at
the view object. There is no access to the
surrounding context or outer controller; all
contextual information must be passed in.
The easiest way to create a Component
is via
a template. If you name a template
app/components/my-foo.hbs
, you will be able to use
{{my-foo}}
in other templates, which will make
an instance of the isolated component.
{{person-profile person=currentUser}}
<h1>{{person.title}}</h1>
<img src={{person.avatar}}>
<p class='signature'>{{person.signature}}</p>
You can use yield
inside a template to
include the contents of any block attached to
the component. The block will be executed in the
context of the surrounding context or outer controller:
{{#person-profile person=currentUser}}
<p>Admin mode</p>
{{! Executed in the controller's context. }}
{{/person-profile}}
<h1>{{person.title}}</h1>
{{! Executed in the component's context. }}
{{yield}} {{! block contents }}
If you want to customize the component, in order to
handle events or actions, you implement a subclass
of Component
named after the name of the
component.
For example, you could implement the action
hello
for the person-profile
component:
import Component from '@ember/component';
export default Component.extend({
actions: {
hello(name) {
console.log("Hello", name);
}
}
});
And then use it in the component's template:
<h1>{{person.title}}</h1>
{{yield}} <!-- block contents -->
<button {{action 'hello' person.name}}>
Say Hello to {{person.name}}
</button>
Components must have a -
in their name to avoid
conflicts with built-in controls that wrap HTML
elements. This is consistent with the same
requirement in web components.
HTML Tag
The default HTML tag name used for a component's DOM representation is div
.
This can be customized by setting the tagName
property.
The following component class:
import Component from '@ember/component';
export default Component.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 component's tag can be set by providing a
classNames
property that is set to an array of strings:
import Component from '@ember/component';
export default Component.extend({
classNames: ['my-class', 'my-other-class']
});
Will result in component 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 component. The return value
of these properties will be added as part of the value for the components's class
attribute. These properties can be computed properties:
import Component from '@ember/component';
import { computed } from '@ember/object';
export default Component.extend({
classNameBindings: ['propertyA', 'propertyB'],
propertyA: 'from-a',
propertyB: computed(function() {
if (someLogic) { return 'from-b'; }
})
});
Will result in component 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
.
import Component from '@ember/component';
export default Component.extend({
classNameBindings: ['hovered'],
hovered: true
});
Will result in component 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:
import Component from '@ember/component';
export default Component.extend({
classNameBindings: ['awesome:so-very-cool'],
awesome: true
});
Will result in component 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:
import Component from '@ember/component';
export default Component.extend({
classNameBindings: ['isUrgent'],
isUrgent: true
});
Will result in component 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 component itself:
import Component from '@ember/component';
import EmberObject from '@ember/object';
export default Component.extend({
classNameBindings: ['messages.empty'],
messages: EmberObject.create({
empty: true
})
});
Will result in component 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:
import Component from '@ember/component';
export default Component.extend({
classNameBindings: ['isEnabled:enabled:disabled'],
isEnabled: true
});
Will result in component 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
:
import Component from '@ember/component';
// Applies no class when isEnabled is true and class 'disabled' when isEnabled is false
export default Component.extend({
classNameBindings: ['isEnabled::disabled'],
isEnabled: true
});
Will result in component instances with an HTML representation of:
<div id="ember1" class="ember-view"></div>
When the isEnabled
property on the component is set to false
, it will result
in component 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 component'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
EmberObject documentation for more
information about concatenated properties.
HTML Attributes
The HTML attribute section of a component's tag can be set by providing an
attributeBindings
property set to an array of property names on the component.
The return value of these properties will be used as the value of the component's
HTML associated attribute:
import Component from '@ember/component';
export default Component.extend({
tagName: 'a',
attributeBindings: ['href'],
href: 'http://google.com'
});
Will result in component 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:
import Component from '@ember/component';
export default Component.extend({
tagName: 'a',
attributeBindings: ['url:href'],
url: 'http://google.com'
});
Will result in component 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:
import Component from '@ember/component';
export default Component.extend({
tagName: 'use',
attributeBindings: ['xlinkHref:xlink:href'],
xlinkHref: '#triangle'
});
Will result in component 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:
import Component from '@ember/component';
export default Component.extend({
tagName: 'input',
attributeBindings: ['disabled'],
disabled: false
});
Will result in a component instance with an HTML representation of:
<input id="ember1" class="ember-view" />
attributeBindings
can refer to computed properties:
import Component from '@ember/component';
import { computed } from '@ember/object';
export default Component.extend({
tagName: 'input',
attributeBindings: ['disabled'],
disabled: 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:
import Component from '@ember/component';
export default Component.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 component's rendered HTML representation.
attributeBindings
is a concatenated property. See EmberObject
documentation for more information about concatenated properties.
Layouts
See Ember.Templates.helpers.yield for more information.
Layout can be used to wrap content in a component. In addition to wrapping content in a Component's template, you can also use the public layout API in your Component JavaScript.
<h1>Person's Title</h1>
<div class='details'>{{yield}}</div>
import Component from '@ember/component';
import layout from '../templates/components/person-profile';
export default Component.extend({
layout
});
The above will result in the following HTML output:
<h1>Person's Title</h1>
<div class="details">
<h2>Chief Basket Weaver</h2>
<h3>Fisherman Industries</h3>
</div>
Responding to Browser Events
Components 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
Components 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.
import Component from '@ember/component';
export default Component.extend({
click(event) {
// will be called when an instance's
// rendered element is clicked
}
});
{{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
didInsertElement public
Inherited from Ember.ViewMixin packages/ember-views/lib/mixins/view_support.js:311
Called when the element of the view has been inserted into the DOM. Override this function to do any set up that requires an element in the document body.
When a view has children, didInsertElement will be called on the child view(s) first and on itself afterwards.
didReceiveAttrs public
Defined in packages/ember-glimmer/lib/component.ts:767
Available since v1.13.0
Called when the attributes passed into the component have been updated. Called both during the initial render of a container and during a rerender. Can be used in place of an observer; code placed here will be executed every time any attribute updates.
didRender public
Defined in packages/ember-glimmer/lib/component.ts:785
Available since v1.13.0
Called after a component has been rendered, both on initial render and in subsequent rerenders.
didUpdate public
Defined in packages/ember-glimmer/lib/component.ts:849
Available since v1.13.0
Called when the component has updated and rerendered itself. Called only during a rerender, not during an initial render.
didUpdateAttrs public
Defined in packages/ember-glimmer/lib/component.ts:817
Available since v1.13.0
Called when the attributes passed into the component have been changed. Called only during a rerender, not during an initial render.
willClearRender public
Inherited from Ember.ViewMixin packages/ember-views/lib/mixins/view_support.js:324
Called when the view is about to rerender, but before anything has been torn down. This is a good opportunity to tear down any manual observers you have installed based on the DOM state
willDestroyElement public
Inherited from Ember.ViewMixin packages/ember-views/lib/mixins/view_support.js:348
Called when the element of the view is going to be destroyed. Override this function to do any teardown that requires an element, like removing event listeners.
Please note: any property changes made during this event will have no effect on object observers.
willInsertElement public
Inherited from Ember.ViewMixin packages/ember-views/lib/mixins/view_support.js:303
Called when a view is going to insert an element into the DOM.
willRender public
Defined in packages/ember-glimmer/lib/component.ts:801
Available since v1.13.0
Called before a component has been rendered, both on initial render and in subsequent rerenders.
willUpdate public
Defined in packages/ember-glimmer/lib/component.ts:833
Available since v1.13.0
Called when the component is about to update and rerender itself. Called only during a rerender, not during an initial render.