1
1
var _ = require ( './util' )
2
2
var config = require ( './config' )
3
- var Observer = require ( './observer' )
3
+ var Dep = require ( './observer/dep ' )
4
4
var expParser = require ( './parsers/expression' )
5
5
var batcher = require ( './batcher' )
6
6
var uid = 0
@@ -18,6 +18,7 @@ var uid = 0
18
18
* - {Boolean} twoWay
19
19
* - {Boolean} deep
20
20
* - {Boolean} user
21
+ * - {Boolean} lazy
21
22
* - {Function} [preProcess]
22
23
* @constructor
23
24
*/
@@ -34,10 +35,12 @@ function Watcher (vm, expOrFn, cb, options) {
34
35
this . deep = ! ! options . deep
35
36
this . user = ! ! options . user
36
37
this . twoWay = ! ! options . twoWay
38
+ this . lazy = ! ! options . lazy
39
+ this . dirty = this . lazy
37
40
this . filters = options . filters
38
41
this . preProcess = options . preProcess
39
42
this . deps = [ ]
40
- this . newDeps = [ ]
43
+ this . newDeps = null
41
44
// parse expression for getter/setter
42
45
if ( isFn ) {
43
46
this . getter = expOrFn
@@ -47,7 +50,9 @@ function Watcher (vm, expOrFn, cb, options) {
47
50
this . getter = res . get
48
51
this . setter = res . set
49
52
}
50
- this . value = this . get ( )
53
+ this . value = this . lazy
54
+ ? undefined
55
+ : this . get ( )
51
56
// state for avoiding false triggers for deep and Array
52
57
// watchers during vm._digest()
53
58
this . queued = this . shallow = false
@@ -147,15 +152,16 @@ p.set = function (value) {
147
152
*/
148
153
149
154
p . beforeGet = function ( ) {
150
- Observer . setTarget ( this )
155
+ Dep . target = this
156
+ this . newDeps = [ ]
151
157
}
152
158
153
159
/**
154
160
* Clean up for dependency collection.
155
161
*/
156
162
157
163
p . afterGet = function ( ) {
158
- Observer . setTarget ( null )
164
+ Dep . target = null
159
165
var i = this . deps . length
160
166
while ( i -- ) {
161
167
var dep = this . deps [ i ]
@@ -164,7 +170,7 @@ p.afterGet = function () {
164
170
}
165
171
}
166
172
this . deps = this . newDeps
167
- this . newDeps = [ ]
173
+ this . newDeps = null
168
174
}
169
175
170
176
/**
@@ -175,7 +181,9 @@ p.afterGet = function () {
175
181
*/
176
182
177
183
p . update = function ( shallow ) {
178
- if ( ! config . async ) {
184
+ if ( this . lazy ) {
185
+ this . dirty = true
186
+ } else if ( ! config . async ) {
179
187
this . run ( )
180
188
} else {
181
189
// if queued, only overwrite shallow with non-shallow,
@@ -214,6 +222,31 @@ p.run = function () {
214
222
}
215
223
}
216
224
225
+ /**
226
+ * Evaluate the value of the watcher.
227
+ * This only gets called for lazy watchers.
228
+ */
229
+
230
+ p . evaluate = function ( ) {
231
+ // avoid overwriting another watcher that is being
232
+ // collected.
233
+ var current = Dep . target
234
+ this . value = this . get ( )
235
+ this . dirty = false
236
+ Dep . target = current
237
+ }
238
+
239
+ /**
240
+ * Depend on all deps collected by this watcher.
241
+ */
242
+
243
+ p . depend = function ( ) {
244
+ var i = this . deps . length
245
+ while ( i -- ) {
246
+ this . deps [ i ] . depend ( )
247
+ }
248
+ }
249
+
217
250
/**
218
251
* Remove self from all dependencies' subcriber list.
219
252
*/
0 commit comments