support watch option alternative syntax

This commit is contained in:
Evan You 2015-06-28 09:24:08 +08:00
parent eb49441ffb
commit 47c52d6bfc
2 changed files with 27 additions and 9 deletions

View File

@ -42,18 +42,19 @@ function registerCallbacks (vm, action, hash) {
* @param {Vue} vm * @param {Vue} vm
* @param {String} action * @param {String} action
* @param {String} key * @param {String} key
* @param {*} handler * @param {Function|String|Object} handler
* @param {Object} [options]
*/ */
function register (vm, action, key, handler) { function register (vm, action, key, handler, options) {
var type = typeof handler var type = typeof handler
if (type === 'function') { if (type === 'function') {
vm[action](key, handler) vm[action](key, handler, options)
} else if (type === 'string') { } else if (type === 'string') {
var methods = vm.$options.methods var methods = vm.$options.methods
var method = methods && methods[handler] var method = methods && methods[handler]
if (method) { if (method) {
vm[action](key, method) vm[action](key, method, options)
} else { } else {
_.warn( _.warn(
'Unknown method: "' + handler + '" when ' + 'Unknown method: "' + handler + '" when ' +
@ -61,6 +62,8 @@ function register (vm, action, key, handler) {
': "' + key + '".' ': "' + key + '".'
) )
} }
} else if (handler && type === 'object') {
register(vm, action, key, handler.handler, handler)
} }
} }

View File

@ -57,25 +57,40 @@ describe('Instance Events', function () {
it('normal', function (done) { it('normal', function (done) {
var spyA = jasmine.createSpy() var spyA = jasmine.createSpy()
var spyB = jasmine.createSpy() var spyB = jasmine.createSpy()
var count = 0
var a = {
b: { c: 1 }
}
var vm = new Vue({ var vm = new Vue({
watch: { watch: {
'a.b.c': spyA, 'a.b.c': spyA,
'b + c': spyB 'b + c': spyB,
a: {
deep: true,
immediate: true,
handler: 'test'
}
}, },
data: { data: {
a: { a: a,
b: { c: 1 }
},
b: 1, b: 1,
c: 2 c: 2
},
methods: {
test: function (val) {
count++
expect(val).toBe(a)
}
} }
}) })
vm.a.b.c = 2 vm.a.b.c = 2
vm.b = 3 vm.b = 3
vm.c = 4 vm.c = 4
expect(count).toBe(1)
_.nextTick(function () { _.nextTick(function () {
expect(spyA).toHaveBeenCalledWith(2, 1) expect(spyA).toHaveBeenCalledWith(2, 1)
expect(spyB).toHaveBeenCalledWith(7, 3) expect(spyB).toHaveBeenCalledWith(7, 3)
expect(count).toBe(2)
done() done()
}) })
}) })
@ -273,4 +288,4 @@ describe('Instance Events', function () {
}) })
}) })