Class Helper
publicimport Helper from '@ember/component/helper';
Ember Helpers are functions that can compute values, and are used in templates.
For example, this code calls a helper named format-currency
:
<Cost @cents={{230}} />
<div>{{format-currency @cents currency="$"}}</div>
Additionally a helper can be called as a nested helper.
In this example, we show the formatted currency value if the showMoney
named argument is truthy.
{{if @showMoney (format-currency @cents currency="$")}}
Helpers defined using a class must provide a compute
function. For example:
import Helper from '@ember/component/helper';
export default class extends Helper {
compute([cents], { currency }) {
return `${currency}${cents * 0.01}`;
}
}
Each time the input to a helper changes, the compute
function will be
called again.
As instances, these helpers also have access to the container and will accept injected dependencies.
Additionally, class helpers can call recompute
to force a new computation.
compute (positional, named) public
Defined in packages/@ember/-internals/glimmer/lib/helper.ts:101
Available since v1.13.0
- positional
- Array
The positional arguments to the helper
- named
- Object
The named arguments to the helper
Override this function when writing a class-based helper.
destroy EmberObject public
Inherited from CoreObject packages/@ember/object/core.ts:541
- returns
- EmberObject
receiver
Destroys an object by setting the isDestroyed
flag and removing its
metadata, which effectively destroys observers and bindings.
If you try to set a property on a destroyed object, an exception will be raised.
Note that destruction is scheduled for the end of the run loop and does not happen immediately. It will set an isDestroying flag immediately.
init public
Inherited from CoreObject packages/@ember/object/core.ts:322
An overridable method called when objects are instantiated. By default, does nothing unless it is overridden during class definition.
Example:
import EmberObject from '@ember/object';
const Person = EmberObject.extend({
init() {
alert(`Name is ${this.get('name')}`);
}
});
let steve = Person.create({
name: 'Steve'
});
// alerts 'Name is Steve'.
NOTE: If you do override init
for a framework class like Component
from @ember/component
, be sure to call this._super(...arguments)
in your init
declaration!
If you don't, Ember may not have an opportunity to
do important setup work, and you'll see strange behavior in your
application.
recompute public
Defined in packages/@ember/-internals/glimmer/lib/helper.ts:138
Available since v1.13.0
On a class-based helper, it may be useful to force a recomputation of that
helpers value. This is akin to rerender
on a component.
For example, this component will rerender when the currentUser
on a
session service changes:
import Helper from '@ember/component/helper'
import { service } from '@ember/service'
import { observer } from '@ember/object'
export default Helper.extend({
session: service(),
onNewUser: observer('session.currentUser', function() {
this.recompute();
}),
compute() {
return this.get('session.currentUser.email');
}
});
toString String public
Inherited from CoreObject packages/@ember/object/core.ts:576
- returns
- String
string representation
Returns a string representation which attempts to provide more information
than Javascript's toString
typically does, in a generic way for all Ember
objects.
import EmberObject from '@ember/object';
const Person = EmberObject.extend();
person = Person.create();
person.toString(); //=> "<Person:ember1024>"
If the object's class is not defined on an Ember namespace, it will indicate it is a subclass of the registered superclass:
const Student = Person.extend();
let student = Student.create();
student.toString(); //=> "<(subclass of Person):ember1025>"
If the method toStringExtension
is defined, its return value will be
included in the output.
const Teacher = Person.extend({
toStringExtension() {
return this.get('fullName');
}
});
teacher = Teacher.create();
teacher.toString(); //=> "<Teacher:ember1026:Tom Dale>"
willDestroy public
Inherited from CoreObject packages/@ember/object/core.ts:568
Override to implement teardown.