Class Helper

public
import 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:

app/templates/application.hbs
<Cost @cents={{230}} />
app/components/cost.hbs
<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:

app/helpers/format-currency.js
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.

Show:

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.

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.

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.

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:

app/helpers/current-user-email.js
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');
  }
});
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>"

Override to implement teardown.