Extends: | Ember.CoreView |
---|---|
Uses: | Ember.ViewStateSupport , |
Defined in: | packages/ember-glimmer/lib/component.js:36 |
Module: | ember |
An Ember.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 an Ember.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 Ember.Component
named after the name of the component.
For example, you could implement the action hello
for the person-profile
component:
import Ember from 'ember'; export default Ember.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.
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 Ember from 'ember'; export default Ember.Component.extend({ tagName: 'em' });
Would result in instances with the following HTML:
<em id="ember1" class="ember-view"></em>
class
AttributeThe 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 Ember from 'ember'; export default Ember.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 Ember from 'ember'; export default Ember.Component.extend({ classNameBindings: ['propertyA', 'propertyB'], propertyA: 'from-a', propertyB: Ember.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 Ember from 'ember'; export default Ember.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 Ember from 'ember'; export default Ember.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 Ember from 'ember'; export default Ember.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 Ember from 'ember'; export default Ember.Component.extend({ classNameBindings: ['messages.empty'], messages: Ember.Object.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 Ember from 'ember'; export default Ember.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 Ember from 'ember'; // Applies no class when isEnabled is true and class 'disabled' when isEnabled is false export default Ember.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 Ember.Object documentation for more information about concatenated properties.
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 Ember from 'ember'; export default Ember.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 Ember from 'ember'; export default Ember.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 Ember from 'ember'; export default Ember.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 Ember from 'ember'; export default Ember.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 Ember from 'ember'; export default Ember.Component.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:
import Ember from 'ember'; export default Ember.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 Ember.Object documentation for more information about concatenated properties.
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 Ember from 'ember'; import layout from '../templates/components/person-profile'; export default Ember.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>
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.
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 Ember from 'ember'; export default Ember.Component.extend({ click(event) { // will be called when an instance's // rendered element is clicked } });
{{action}}
HelperSee Ember.Templates.helpers.action.
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
© 2017 Yehuda Katz, Tom Dale and Ember.js contributors
Licensed under the MIT License.
https://emberjs.com/api/ember/2.15/classes/Ember.Component