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 {String} action
* @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
if (type === 'function') {
vm[action](key, handler)
vm[action](key, handler, options)
} else if (type === 'string') {
var methods = vm.$options.methods
var method = methods && methods[handler]
if (method) {
vm[action](key, method)
vm[action](key, method, options)
} else {
_.warn(
'Unknown method: "' + handler + '" when ' +
@ -61,6 +62,8 @@ function register (vm, action, key, handler) {
': "' + 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) {
var spyA = jasmine.createSpy()
var spyB = jasmine.createSpy()
var count = 0
var a = {
b: { c: 1 }
}
var vm = new Vue({
watch: {
'a.b.c': spyA,
'b + c': spyB
'b + c': spyB,
a: {
deep: true,
immediate: true,
handler: 'test'
}
},
data: {
a: {
b: { c: 1 }
},
a: a,
b: 1,
c: 2
},
methods: {
test: function (val) {
count++
expect(val).toBe(a)
}
}
})
vm.a.b.c = 2
vm.b = 3
vm.c = 4
expect(count).toBe(1)
_.nextTick(function () {
expect(spyA).toHaveBeenCalledWith(2, 1)
expect(spyB).toHaveBeenCalledWith(7, 3)
expect(count).toBe(2)
done()
})
})
@ -273,4 +288,4 @@ describe('Instance Events', function () {
})
})
})