Skip to content

Commit 47c52d6

Browse files
committed
support watch option alternative syntax
1 parent eb49441 commit 47c52d6

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

src/instance/events.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,25 +42,28 @@ function registerCallbacks (vm, action, hash) {
4242
* @param {Vue} vm
4343
* @param {String} action
4444
* @param {String} key
45-
* @param {*} handler
45+
* @param {Function|String|Object} handler
46+
* @param {Object} [options]
4647
*/
4748

48-
function register (vm, action, key, handler) {
49+
function register (vm, action, key, handler, options) {
4950
var type = typeof handler
5051
if (type === 'function') {
51-
vm[action](key, handler)
52+
vm[action](key, handler, options)
5253
} else if (type === 'string') {
5354
var methods = vm.$options.methods
5455
var method = methods && methods[handler]
5556
if (method) {
56-
vm[action](key, method)
57+
vm[action](key, method, options)
5758
} else {
5859
_.warn(
5960
'Unknown method: "' + handler + '" when ' +
6061
'registering callback for ' + action +
6162
': "' + key + '".'
6263
)
6364
}
65+
} else if (handler && type === 'object') {
66+
register(vm, action, key, handler.handler, handler)
6467
}
6568
}
6669

test/unit/specs/instance/events_spec.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,25 +57,40 @@ describe('Instance Events', function () {
5757
it('normal', function (done) {
5858
var spyA = jasmine.createSpy()
5959
var spyB = jasmine.createSpy()
60+
var count = 0
61+
var a = {
62+
b: { c: 1 }
63+
}
6064
var vm = new Vue({
6165
watch: {
6266
'a.b.c': spyA,
63-
'b + c': spyB
67+
'b + c': spyB,
68+
a: {
69+
deep: true,
70+
immediate: true,
71+
handler: 'test'
72+
}
6473
},
6574
data: {
66-
a: {
67-
b: { c: 1 }
68-
},
75+
a: a,
6976
b: 1,
7077
c: 2
78+
},
79+
methods: {
80+
test: function (val) {
81+
count++
82+
expect(val).toBe(a)
83+
}
7184
}
7285
})
7386
vm.a.b.c = 2
7487
vm.b = 3
7588
vm.c = 4
89+
expect(count).toBe(1)
7690
_.nextTick(function () {
7791
expect(spyA).toHaveBeenCalledWith(2, 1)
7892
expect(spyB).toHaveBeenCalledWith(7, 3)
93+
expect(count).toBe(2)
7994
done()
8095
})
8196
})
@@ -273,4 +288,4 @@ describe('Instance Events', function () {
273288

274289
})
275290

276-
})
291+
})

0 commit comments

Comments
 (0)