Skip to content

Commit 8d545da

Browse files
committed
refactor watcher to use an option object
1 parent abdc462 commit 8d545da

File tree

5 files changed

+33
-16
lines changed

5 files changed

+33
-16
lines changed

src/api/data.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ exports.$watch = function (exp, cb, deep, immediate) {
7777
}
7878
if (!watcher) {
7979
watcher = vm._userWatchers[key] =
80-
new Watcher(vm, exp, wrappedCb, null, false, deep)
80+
new Watcher(vm, exp, wrappedCb, {
81+
deep: deep
82+
})
8183
} else {
8284
watcher.addCb(wrappedCb)
8385
}

src/directive.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,11 @@ p._bind = function (def) {
8585
this.vm,
8686
this._watcherExp,
8787
update, // callback
88-
this.filters,
89-
this.twoWay, // need setter,
90-
this.deep
88+
{
89+
filters: this.filters,
90+
twoWay: this.twoWay,
91+
deep: this.deep
92+
}
9193
)
9294
} else {
9395
watcher.addCb(update)

src/directives/model/select.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ function initOptions (expression) {
7070
this.vm,
7171
expression,
7272
optionUpdateWatcher,
73-
null, false, true
73+
{ deep: true }
7474
)
7575
// update with initial value
7676
optionUpdateWatcher(this.optionWatcher.value)

src/watcher.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,35 @@ var uid = 0
1515
* @param {Vue} vm
1616
* @param {String} expression
1717
* @param {Function} cb
18-
* @param {Array} [filters]
19-
* @param {Boolean} [needSet]
20-
* @param {Boolean} [deep]
18+
* @param {Object} options
19+
* - {Array} filters
20+
* - {Boolean} twoWay
21+
* - {Boolean} deep
22+
* - {Boolean} user
2123
* @constructor
2224
*/
2325

24-
function Watcher (vm, expression, cb, filters, needSet, deep) {
26+
function Watcher (vm, expression, cb, options) {
2527
this.vm = vm
2628
vm._watcherList.push(this)
2729
this.expression = expression
2830
this.cbs = [cb]
2931
this.id = ++uid // uid for batching
3032
this.active = true
31-
this.deep = deep
33+
options = options || {}
34+
this.deep = options.deep
35+
this.user = options.user
3236
this.deps = Object.create(null)
3337
// setup filters if any.
3438
// We delegate directive filters here to the watcher
3539
// because they need to be included in the dependency
3640
// collection process.
37-
this.readFilters = filters && filters.read
38-
this.writeFilters = filters && filters.write
41+
if (options.filters) {
42+
this.readFilters = options.filters.read
43+
this.writeFilters = options.filters.write
44+
}
3945
// parse expression for getter/setter
40-
var res = expParser.parse(expression, needSet)
46+
var res = expParser.parse(expression, options.twoWay)
4147
this.getter = res.get
4248
this.setter = res.set
4349
this.value = this.get()

test/unit/specs/watcher_spec.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,9 @@ describe('Watcher', function () {
239239
{ name: 'test', args: [3] },
240240
{ name: 'test2', args: ['yo']}
241241
])
242-
var watcher = new Watcher(vm, 'b.c', spy, filters)
242+
var watcher = new Watcher(vm, 'b.c', spy, {
243+
filters: filters
244+
})
243245
expect(watcher.value).toBe('6yo')
244246
vm.b.c = 3
245247
nextTick(function () {
@@ -258,7 +260,10 @@ describe('Watcher', function () {
258260
var filters = _.resolveFilters(vm, [
259261
{ name: 'test', args: [5] }
260262
])
261-
var watcher = new Watcher(vm, 'b["c"]', spy, filters, true)
263+
var watcher = new Watcher(vm, 'b["c"]', spy, {
264+
filters: filters,
265+
twoWay: true
266+
})
262267
expect(watcher.value).toBe(2)
263268
watcher.set(4) // shoud not change the value
264269
nextTick(function () {
@@ -288,7 +293,9 @@ describe('Watcher', function () {
288293
})
289294

290295
it('deep watch', function (done) {
291-
var watcher = new Watcher(vm, 'b', spy, null, false, true)
296+
var watcher = new Watcher(vm, 'b', spy, {
297+
deep: true
298+
})
292299
vm.b.c = { d: 4 }
293300
nextTick(function () {
294301
expect(spy).toHaveBeenCalledWith(vm.b, vm.b)

0 commit comments

Comments
 (0)