+39
-5
lines changedFilter options
+39
-5
lines changed Original file line number Diff line number Diff line change
@@ -247,16 +247,20 @@ function initWatch (vm: Component, watch: Object) {
247
247
}
248
248
}
249
249
250
-
function createWatcher (vm: Component, key: string, handler: any) {
251
-
let options
250
+
function createWatcher (
251
+
vm: Component,
252
+
keyOrFn: string | Function,
253
+
handler: any,
254
+
options?: Object
255
+
) {
252
256
if (isPlainObject(handler)) {
253
257
options = handler
254
258
handler = handler.handler
255
259
}
256
260
if (typeof handler === 'string') {
257
261
handler = vm[handler]
258
262
}
259
-
vm.$watch(key, handler, options)
263
+
return vm.$watch(keyOrFn, handler, options)
260
264
}
261
265
262
266
export function stateMixin (Vue: Class<Component>) {
@@ -287,10 +291,13 @@ export function stateMixin (Vue: Class<Component>) {
287
291
288
292
Vue.prototype.$watch = function (
289
293
expOrFn: string | Function,
290
-
cb: Function,
294
+
cb: any,
291
295
options?: Object
292
296
): Function {
293
297
const vm: Component = this
298
+
if (isPlainObject(cb)) {
299
+
return createWatcher(vm, expOrFn, cb, options)
300
+
}
294
301
options = options || {}
295
302
options.user = true
296
303
const watcher = new Watcher(vm, expOrFn, cb, options)
Original file line number Diff line number Diff line change
@@ -21,14 +21,17 @@ describe('Instance methods data', () => {
21
21
describe('$watch', () => {
22
22
let vm, spy
23
23
beforeEach(() => {
24
+
spy = jasmine.createSpy('watch')
24
25
vm = new Vue({
25
26
data: {
26
27
a: {
27
28
b: 1
28
29
}
30
+
},
31
+
methods: {
32
+
foo: spy
29
33
}
30
34
})
31
-
spy = jasmine.createSpy('watch')
32
35
})
33
36
34
37
it('basic usage', done => {
@@ -81,6 +84,30 @@ describe('Instance methods data', () => {
81
84
}).then(done)
82
85
})
83
86
87
+
it('handler option', done => {
88
+
var oldA = vm.a
89
+
vm.$watch('a', {
90
+
handler: spy,
91
+
deep: true
92
+
})
93
+
vm.a.b = 2
94
+
waitForUpdate(() => {
95
+
expect(spy).toHaveBeenCalledWith(oldA, oldA)
96
+
vm.a = { b: 3 }
97
+
}).then(() => {
98
+
expect(spy).toHaveBeenCalledWith(vm.a, oldA)
99
+
}).then(done)
100
+
})
101
+
102
+
it('handler option in string', () => {
103
+
vm.$watch('a.b', {
104
+
handler: 'foo',
105
+
immediate: true
106
+
})
107
+
expect(spy.calls.count()).toBe(1)
108
+
expect(spy).toHaveBeenCalledWith(1)
109
+
})
110
+
84
111
it('warn expresssion', () => {
85
112
vm.$watch('a + b', spy)
86
113
expect('Watcher only accepts simple dot-delimited paths').toHaveBeenWarned()
You can’t perform that action at this time.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4