Helpers can also be defined using class syntax. For instance, we could define the substring helper using classes
instead.
app/helpers/substring.js
:
import { helper } from '@ember/component/helper';
import Helper from '@ember/component/helper';
function substring([string], { start, length }) {
export default class Substring extends Helper {
compute([string], { start, end }) {
return string.substring(start || 0, end);
}
}
Class helpers
are useful when the helper logic is fairly complicated, requires fine-grained control of the helper lifecycle, or is stateful (we'll be discussing state in the next chapter).