Class Component
publicExtends: | Ember.CoreView |
---|---|
Uses: | Ember.ViewStateSupport , | Ember.ViewMixin , Ember.ActionSupport , Ember.ClassNamesSupport , Ember.TargetActionSupport
Defined in: | packages/ember-glimmer/lib/component.ts:35 |
Module: | @ember/component |
import 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.
foo.hbs
{{person-profile person=currentUser}}
profile.hbs
<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}}
profile.hbs
<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:
profile.js
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:
profile.hbs
<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:
paragraph.js
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:
widget.js
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:
widget.js
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
.
widget.js
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:
widget.js
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:
widget.js
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:
widget.js
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:
widget.js
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
:
widget.js
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:
anchor.js
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:
anchor.js
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:
use.js
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:
input.js
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:
input.js
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:
input.js
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.
profile.hbs
<h1>Person's Title</h1> <div class='details'>{{yield}}</div>
profile.js
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.
widget.js
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
Methods
- $
- addObserver
- cacheFor
- decrementProperty
- destroy
- didReceiveAttrs
- didRender
- didUpdate
- didUpdateAttrs
- get
- getProperties
- getWithDefault
- has
- incrementProperty
- init
- notifyPropertyChange
- off
- on
- one
- readDOMAttr
- removeObserver
- rerender
- send
- sendAction
- set
- setProperties
- toString
- toggleProperty
- trigger
- willDestroy
- willRender
- willUpdate
Properties
- actions
- ariaRole
- attributeBindings
- classNameBindings
- classNames
- concatenatedProperties
- element
- elementId
- isDestroyed
- isDestroying
- isVisible
- layout
- mergedProperties
- positionalParams
- tagName
Events
© 2020 Yehuda Katz, Tom Dale and Ember.js contributors
Licensed under the MIT License.
https://api.emberjs.com/ember/2.18/classes/Component