Skip to content

Commit c48c709

Browse files
committed
support watching a function
1 parent a94c80b commit c48c709

File tree

3 files changed

+49
-7
lines changed

3 files changed

+49
-7
lines changed

src/watcher.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ var uid = 0
2222
* @constructor
2323
*/
2424

25-
function Watcher (vm, expression, cb, options) {
25+
function Watcher (vm, expOrFn, cb, options) {
26+
var isFn = typeof expOrFn === 'function'
2627
this.vm = vm
2728
vm._watchers.push(this)
28-
this.expression = expression
29+
this.expression = isFn ? '' : expOrFn
2930
this.cb = cb
3031
this.id = ++uid // uid for batching
3132
this.active = true
@@ -38,9 +39,14 @@ function Watcher (vm, expression, cb, options) {
3839
this.deps = []
3940
this.newDeps = []
4041
// parse expression for getter/setter
41-
var res = expParser.parse(expression, options.twoWay)
42-
this.getter = res.get
43-
this.setter = res.set
42+
if (isFn) {
43+
this.getter = expOrFn
44+
this.setter = undefined
45+
} else {
46+
var res = expParser.parse(expOrFn, options.twoWay)
47+
this.getter = res.get
48+
this.setter = res.set
49+
}
4450
this.value = this.get()
4551
}
4652

test/unit/specs/api/data_spec.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,26 @@ describe('Data API', function () {
104104
})
105105
})
106106

107+
it('function $watch', function (done) {
108+
var spy = jasmine.createSpy()
109+
// test immediate invoke
110+
var unwatch = vm.$watch(function () {
111+
return this.a + this.b.c
112+
}, spy, false, true)
113+
expect(spy).toHaveBeenCalledWith(3, undefined)
114+
vm.a = 2
115+
nextTick(function () {
116+
expect(spy).toHaveBeenCalledWith(4, 3)
117+
// unwatch
118+
unwatch()
119+
vm.a = 3
120+
nextTick(function () {
121+
expect(spy.calls.count()).toBe(2)
122+
done()
123+
})
124+
})
125+
})
126+
107127
it('deep $watch', function (done) {
108128
var oldB = vm.b
109129
var spy = jasmine.createSpy()
@@ -164,4 +184,4 @@ function leftHandThrows () {
164184
} catch (e) {
165185
return true
166186
}
167-
}
187+
}

test/unit/specs/watcher_spec.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,22 @@ describe('Watcher', function () {
313313
})
314314
})
315315

316+
it('watch function', function (done) {
317+
var watcher = new Watcher(vm, function () {
318+
return this.a + this.b.d
319+
}, spy)
320+
expect(watcher.value).toBe(5)
321+
vm.a = 2
322+
nextTick(function () {
323+
expect(spy).toHaveBeenCalledWith(6, 5)
324+
vm.b = { d: 2 }
325+
nextTick(function () {
326+
expect(spy).toHaveBeenCalledWith(4, 6)
327+
done()
328+
})
329+
})
330+
})
331+
316332
it('teardown', function (done) {
317333
var watcher = new Watcher(vm, 'b.c', spy)
318334
watcher.teardown()
@@ -348,4 +364,4 @@ describe('Watcher', function () {
348364
expect(hasWarned(_, 'Error when evaluating setter')).toBe(true)
349365
})
350366

351-
})
367+
})

0 commit comments

Comments
 (0)