Skip to content

Commit

Permalink
refactor watcher to use an option object
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Dec 5, 2014
1 parent abdc462 commit 8d545da
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 16 deletions.
4 changes: 3 additions & 1 deletion src/api/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ exports.$watch = function (exp, cb, deep, immediate) {
}
if (!watcher) {
watcher = vm._userWatchers[key] =
new Watcher(vm, exp, wrappedCb, null, false, deep)
new Watcher(vm, exp, wrappedCb, {
deep: deep
})
} else {
watcher.addCb(wrappedCb)
}
Expand Down
8 changes: 5 additions & 3 deletions src/directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,11 @@ p._bind = function (def) {
this.vm,
this._watcherExp,
update, // callback
this.filters,
this.twoWay, // need setter,
this.deep
{
filters: this.filters,
twoWay: this.twoWay,
deep: this.deep
}
)
} else {
watcher.addCb(update)
Expand Down
2 changes: 1 addition & 1 deletion src/directives/model/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function initOptions (expression) {
this.vm,
expression,
optionUpdateWatcher,
null, false, true
{ deep: true }
)
// update with initial value
optionUpdateWatcher(this.optionWatcher.value)
Expand Down
22 changes: 14 additions & 8 deletions src/watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,35 @@ var uid = 0
* @param {Vue} vm
* @param {String} expression
* @param {Function} cb
* @param {Array} [filters]
* @param {Boolean} [needSet]
* @param {Boolean} [deep]
* @param {Object} options
* - {Array} filters
* - {Boolean} twoWay
* - {Boolean} deep
* - {Boolean} user
* @constructor
*/

function Watcher (vm, expression, cb, filters, needSet, deep) {
function Watcher (vm, expression, cb, options) {
this.vm = vm
vm._watcherList.push(this)
this.expression = expression
this.cbs = [cb]
this.id = ++uid // uid for batching
this.active = true
this.deep = deep
options = options || {}
this.deep = options.deep
this.user = options.user
this.deps = Object.create(null)
// setup filters if any.
// We delegate directive filters here to the watcher
// because they need to be included in the dependency
// collection process.
this.readFilters = filters && filters.read
this.writeFilters = filters && filters.write
if (options.filters) {
this.readFilters = options.filters.read
this.writeFilters = options.filters.write
}
// parse expression for getter/setter
var res = expParser.parse(expression, needSet)
var res = expParser.parse(expression, options.twoWay)
this.getter = res.get
this.setter = res.set
this.value = this.get()
Expand Down
13 changes: 10 additions & 3 deletions test/unit/specs/watcher_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,9 @@ describe('Watcher', function () {
{ name: 'test', args: [3] },
{ name: 'test2', args: ['yo']}
])
var watcher = new Watcher(vm, 'b.c', spy, filters)
var watcher = new Watcher(vm, 'b.c', spy, {
filters: filters
})
expect(watcher.value).toBe('6yo')
vm.b.c = 3
nextTick(function () {
Expand All @@ -258,7 +260,10 @@ describe('Watcher', function () {
var filters = _.resolveFilters(vm, [
{ name: 'test', args: [5] }
])
var watcher = new Watcher(vm, 'b["c"]', spy, filters, true)
var watcher = new Watcher(vm, 'b["c"]', spy, {
filters: filters,
twoWay: true
})
expect(watcher.value).toBe(2)
watcher.set(4) // shoud not change the value
nextTick(function () {
Expand Down Expand Up @@ -288,7 +293,9 @@ describe('Watcher', function () {
})

it('deep watch', function (done) {
var watcher = new Watcher(vm, 'b', spy, null, false, true)
var watcher = new Watcher(vm, 'b', spy, {
deep: true
})
vm.b.c = { d: 4 }
nextTick(function () {
expect(spy).toHaveBeenCalledWith(vm.b, vm.b)
Expand Down

0 comments on commit 8d545da

Please sign in to comment.