-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
helper.ts
146 lines (116 loc) · 3.56 KB
/
helper.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/**
@module @ember/component
*/
import { Dict, Opaque } from '@glimmer/util';
import { DirtyableTag } from '@glimmer/reference';
import { FrameworkObject } from 'ember-runtime';
import { symbol } from 'ember-utils';
export const RECOMPUTE_TAG = symbol('RECOMPUTE_TAG');
/**
Ember Helpers are functions that can compute values, and are used in templates.
For example, this code calls a helper named `format-currency`:
```handlebars
<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`:
```handlebars
{{show-money amount=(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 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 an will accept
injected dependencies.
Additionally, class helpers can call `recompute` to force a new computation.
@class Helper
@public
@since 1.13.0
*/
let Helper = FrameworkObject.extend({
isHelperInstance: true,
init() {
this._super(...arguments);
this[RECOMPUTE_TAG] = new DirtyableTag();
},
/**
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 { inject as 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');
}
});
```
@method recompute
@public
@since 1.13.0
*/
recompute() {
this[RECOMPUTE_TAG].dirty();
},
/**
Override this function when writing a class-based helper.
@method compute
@param {Array} params The positional arguments to the helper
@param {Object} hash The named arguments to the helper
@public
@since 1.13.0
*/
});
Helper.reopenClass({
isHelperFactory: true,
});
export class SimpleHelper {
isHelperFactory = true;
isHelperInstance = true;
isSimpleHelperFactory = true;
constructor(public compute: (positional: any[], named: Dict<Opaque>) => any) { }
create() {
return this;
}
}
/**
In many cases, the ceremony of a full `Helper` class is not required.
The `helper` method create pure-function helpers without instances. For
example:
```app/helpers/format-currency.js
import { helper } from '@ember/component/helper';
export default helper(function(params, hash) {
let cents = params[0];
let currency = hash.currency;
return `${currency}${cents * 0.01}`;
});
```
@static
@param {Function} helper The helper function
@method helper
@for @ember/component/helper
@public
@since 1.13.0
*/
export function helper(helperFn: (params: any[], hash?: any) => string) {
return new SimpleHelper(helperFn);
}
export default Helper;