Helper

Ember.Helper Class

PUBLIC

Defined in: packages/ember-htmlbars/lib/helper.js:8

Module: ember-templates

Ember Helpers are functions that can compute values, and are used in templates. For example, this code calls a helper named format-currency:

<div>{{format-currency cents currency="$"}}</div>

Additionally, a helper can be called as a nested helper (sometimes called a subexpression). In this example, the computed value of a helper is passed to a component named show-money:

{{show-money amount=(format-currency cents currency="$")}}

Helpers defined using a class must provide a compute function. For example:

export default Ember.Helper.extend({
  compute(params, hash) {
    let cents = params[0];
    let currency = hash.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.

If the output of your helper is only dependent on the current input, then you can use the Helper.helper function. See Ember.Helper.helper.

In this form the example above becomes:

export default Ember.Helper.helper((params, hash) => {
  let cents = params[0];
  let currency = hash.currency;
  return `${currency}${cents * 0.01}`;
});
doc_EmberJs
2016-11-30 16:52:11
Comments
Leave a Comment

Please login to continue.