11var _ = require ( './util' )
22var config = require ( './config' )
3- var Observer = require ( './observer' )
3+ var Dep = require ( './observer/dep ' )
44var expParser = require ( './parsers/expression' )
55var batcher = require ( './batcher' )
66var uid = 0
@@ -18,6 +18,7 @@ var uid = 0
1818 * - {Boolean} twoWay
1919 * - {Boolean} deep
2020 * - {Boolean} user
21+ * - {Boolean} lazy
2122 * - {Function} [preProcess]
2223 * @constructor
2324 */
@@ -34,10 +35,12 @@ function Watcher (vm, expOrFn, cb, options) {
3435 this . deep = ! ! options . deep
3536 this . user = ! ! options . user
3637 this . twoWay = ! ! options . twoWay
38+ this . lazy = ! ! options . lazy
39+ this . dirty = this . lazy
3740 this . filters = options . filters
3841 this . preProcess = options . preProcess
3942 this . deps = [ ]
40- this . newDeps = [ ]
43+ this . newDeps = null
4144 // parse expression for getter/setter
4245 if ( isFn ) {
4346 this . getter = expOrFn
@@ -47,7 +50,9 @@ function Watcher (vm, expOrFn, cb, options) {
4750 this . getter = res . get
4851 this . setter = res . set
4952 }
50- this . value = this . get ( )
53+ this . value = this . lazy
54+ ? undefined
55+ : this . get ( )
5156 // state for avoiding false triggers for deep and Array
5257 // watchers during vm._digest()
5358 this . queued = this . shallow = false
@@ -147,15 +152,16 @@ p.set = function (value) {
147152 */
148153
149154p . beforeGet = function ( ) {
150- Observer . setTarget ( this )
155+ Dep . target = this
156+ this . newDeps = [ ]
151157}
152158
153159/**
154160 * Clean up for dependency collection.
155161 */
156162
157163p . afterGet = function ( ) {
158- Observer . setTarget ( null )
164+ Dep . target = null
159165 var i = this . deps . length
160166 while ( i -- ) {
161167 var dep = this . deps [ i ]
@@ -164,7 +170,7 @@ p.afterGet = function () {
164170 }
165171 }
166172 this . deps = this . newDeps
167- this . newDeps = [ ]
173+ this . newDeps = null
168174}
169175
170176/**
@@ -175,7 +181,9 @@ p.afterGet = function () {
175181 */
176182
177183p . update = function ( shallow ) {
178- if ( ! config . async ) {
184+ if ( this . lazy ) {
185+ this . dirty = true
186+ } else if ( ! config . async ) {
179187 this . run ( )
180188 } else {
181189 // if queued, only overwrite shallow with non-shallow,
@@ -214,6 +222,31 @@ p.run = function () {
214222 }
215223}
216224
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+
217250/**
218251 * Remove self from all dependencies' subcriber list.
219252 */
0 commit comments