Function

Module: @ember/helper
context
Object
The parent context of the helper
definition
Object
The helper definition
computeArgs
Function
An optional function that produces args
returns

The invokeHelper function can be used to create a helper instance in JavaScript.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// app/components/data-loader.js
import Component from '@glimmer/component';
import Helper from '@ember/component/helper';
import { invokeHelper } from '@ember/helper';

class PlusOne extends Helper {
  compute([num]) {
    return number + 1;
  }
}

export default class PlusOne extends Component {
  plusOne = invokeHelper(this, RemoteData, () => {
    return {
      positional: [this.args.number],
    };
  });
}
1
{{this.plusOne.value}}

It receives three arguments:

  • context: The parent context of the helper. When the parent is torn down and removed, the helper will be as well.
  • definition: The definition of the helper.
  • computeArgs: An optional function that produces the arguments to the helper. The function receives the parent context as an argument, and must return an object with a positional property that is an array and/or a named property that is an object.

And it returns a Cache instance that contains the most recent value of the helper. You can access the helper using getValue() like any other cache. The cache is also destroyable, and using the destroy() function on it will cause the helper to be torn down.

Note that using getValue() on helpers that have scheduled effects will not trigger the effect early. Effects will continue to run at their scheduled time.