-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
Copy pathfunction.js
196 lines (156 loc) · 5.48 KB
/
function.js
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/**
@module ember
*/
import { ENV } from 'ember-environment';
import {
on,
computed,
observer
} from 'ember-metal';
import { assert, deprecateFunc } from 'ember-debug';
const FunctionPrototype = Function.prototype;
if (ENV.EXTEND_PROTOTYPES.Function) {
/**
The `property` extension of Javascript's Function prototype is available
when `EmberENV.EXTEND_PROTOTYPES` or `EmberENV.EXTEND_PROTOTYPES.Function` is
`true`, which is the default.
Computed properties allow you to treat a function like a property:
```app/utils/president.js
import EmberObject from '@ember/object';
export default EmberObject.extend({
firstName: '',
lastName: '',
fullName: function() {
return this.get('firstName') + ' ' + this.get('lastName');
}.property() // Call this flag to mark the function as a property
});
```
```javascript
let president = President.create({
firstName: 'Barack',
lastName: 'Obama'
});
president.get('fullName'); // 'Barack Obama'
```
Treating a function like a property is useful because they can work with
bindings, just like any other property.
Many computed properties have dependencies on other properties. For
example, in the above example, the `fullName` property depends on
`firstName` and `lastName` to determine its value. You can tell Ember
about these dependencies like this:
```app/utils/president.js
import EmberObject from '@ember/object';
export default EmberObject.extend({
firstName: '',
lastName: '',
fullName: function() {
return this.get('firstName') + ' ' + this.get('lastName');
// Tell Ember.js that this computed property depends on firstName
// and lastName
}.property('firstName', 'lastName')
});
```
Make sure you list these dependencies so Ember knows when to update
bindings that connect to a computed property. Changing a dependency
will not immediately trigger an update of the computed property, but
will instead clear the cache so that it is updated when the next `get`
is called on the property.
See [Ember.ComputedProperty](/api/classes/Ember.ComputedProperty.html), [Ember.computed](/api/classes/Ember.computed.html).
@method property
@for Function
@public
*/
FunctionPrototype.property = function () {
return computed(...arguments, this);
};
/**
The `observes` extension of Javascript's Function prototype is available
when `EmberENV.EXTEND_PROTOTYPES` or `EmberENV.EXTEND_PROTOTYPES.Function` is
true, which is the default.
You can observe property changes simply by adding the `observes`
call to the end of your method declarations in classes that you write.
For example:
```javascript
import EmberObject from '@ember/object';
EmberObject.extend({
valueObserver: function() {
// Executes whenever the "value" property changes
}.observes('value')
});
```
In the future this method may become asynchronous.
See `observer`.
@method observes
@for Function
@public
*/
FunctionPrototype.observes = function() {
return observer(...arguments, this);
};
FunctionPrototype._observesImmediately = function () {
assert(
'Immediate observers must observe internal properties only, ' +
'not properties on other objects.',
function checkIsInternalProperty() {
for (let i = 0; i < arguments.length; i++) {
if (arguments[i].indexOf('.') !== -1) {
return false;
}
}
return true;
}
);
// observes handles property expansion
return this.observes(...arguments);
};
/**
The `observesImmediately` extension of Javascript's Function prototype is
available when `EmberENV.EXTEND_PROTOTYPES` or
`EmberENV.EXTEND_PROTOTYPES.Function` is true, which is the default.
You can observe property changes simply by adding the `observesImmediately`
call to the end of your method declarations in classes that you write.
For example:
```javascript
import EmberObject from '@ember/object';
EmberObject.extend({
valueObserver: function() {
// Executes immediately after the "value" property changes
}.observesImmediately('value')
});
```
In the future, `observes` may become asynchronous. In this event,
`observesImmediately` will maintain the synchronous behavior.
See `Ember.immediateObserver`.
@method observesImmediately
@for Function
@deprecated
@private
*/
FunctionPrototype.observesImmediately = deprecateFunc(
'Function#observesImmediately is deprecated. Use Function#observes instead',
{ id: 'ember-runtime.ext-function', until: '3.0.0' },
FunctionPrototype._observesImmediately
);
/**
The `on` extension of Javascript's Function prototype is available
when `EmberENV.EXTEND_PROTOTYPES` or `EmberENV.EXTEND_PROTOTYPES.Function` is
true, which is the default.
You can listen for events simply by adding the `on` call to the end of
your method declarations in classes or mixins that you write. For example:
```javascript
import Mixin from '@ember/mixin';
Mixin.create({
doSomethingWithElement: function() {
// Executes whenever the "didInsertElement" event fires
}.on('didInsertElement')
});
```
See `Ember.on`.
@method on
@for Function
@public
*/
FunctionPrototype.on = function () {
return on(...arguments, this);
};
}