Skip to content

Commit caca583

Browse files
committed
let vm.$watch take an options object instead of unnamed arguments
1 parent 7b28cb2 commit caca583

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

src/api/data.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,21 +62,23 @@ exports.$delete = function (key) {
6262
*
6363
* @param {String} exp
6464
* @param {Function} cb
65-
* @param {Boolean} [deep]
66-
* @param {Boolean} [immediate]
65+
* @param {Object} [options]
66+
* - {Boolean} deep
67+
* - {Boolean} immediate
68+
* - {Boolean} user
6769
* @return {Function} - unwatchFn
6870
*/
6971

70-
exports.$watch = function (exp, cb, deep, immediate) {
72+
exports.$watch = function (exp, cb, options) {
7173
var vm = this
7274
var wrappedCb = function (val, oldVal) {
7375
cb.call(vm, val, oldVal)
7476
}
7577
var watcher = new Watcher(vm, exp, wrappedCb, {
76-
deep: deep,
77-
user: true
78+
deep: options && options.deep,
79+
user: !options || options.user !== false
7880
})
79-
if (immediate) {
81+
if (options && options.immediate) {
8082
wrappedCb(watcher.value)
8183
}
8284
return function unwatchFn () {
@@ -147,4 +149,4 @@ exports.$log = function (path) {
147149
data = JSON.parse(JSON.stringify(data))
148150
}
149151
console.log(data)
150-
}
152+
}

src/api/global.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,4 @@ function createAssetRegisters (Constructor) {
136136
}
137137
}
138138

139-
createAssetRegisters(exports)
139+
createAssetRegisters(exports)

test/unit/specs/api/data_spec.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ describe('Data API', function () {
8989
it('$watch', function (done) {
9090
var spy = jasmine.createSpy()
9191
// test immediate invoke
92-
var unwatch = vm.$watch('a + b.c', spy, false, true)
92+
var unwatch = vm.$watch('a + b.c', spy, {
93+
immediate: true
94+
})
9395
expect(spy).toHaveBeenCalledWith(3, undefined)
9496
vm.a = 2
9597
nextTick(function () {
@@ -109,7 +111,7 @@ describe('Data API', function () {
109111
// test immediate invoke
110112
var unwatch = vm.$watch(function () {
111113
return this.a + this.b.c
112-
}, spy, false, true)
114+
}, spy, { immediate: true })
113115
expect(spy).toHaveBeenCalledWith(3, undefined)
114116
vm.a = 2
115117
nextTick(function () {
@@ -127,7 +129,9 @@ describe('Data API', function () {
127129
it('deep $watch', function (done) {
128130
var oldB = vm.b
129131
var spy = jasmine.createSpy()
130-
vm.$watch('b', spy, true)
132+
vm.$watch('b', spy, {
133+
deep: true
134+
})
131135
vm.b.c = 3
132136
nextTick(function () {
133137
expect(spy).toHaveBeenCalledWith(oldB, oldB)

0 commit comments

Comments
 (0)