Skip to content

Commit

Permalink
binding check dir/subs count before queueing, rename instances -> dirs
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Feb 14, 2014
1 parent 48e328c commit 1a3ddfd
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 21 deletions.
16 changes: 9 additions & 7 deletions src/binding.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function Binding (compiler, key, isExp, isFn) {
this.root = !this.isExp && key.indexOf('.') === -1
this.compiler = compiler
this.key = key
this.instances = []
this.dirs = []
this.subs = []
this.deps = []
this.unbound = false
Expand All @@ -31,17 +31,19 @@ BindingProto.update = function (value) {
if (!this.isComputed || this.isFn) {
this.value = value
}
batcher.queue(this)
if (this.dirs.length || this.subs.length) {
batcher.queue(this)
}
}

/**
* Actually update the instances.
* Actually update the directives.
*/
BindingProto._update = function () {
var i = this.instances.length,
var i = this.dirs.length,
value = this.val()
while (i--) {
this.instances[i].update(value)
this.dirs[i].update(value)
}
this.pub()
}
Expand Down Expand Up @@ -76,9 +78,9 @@ BindingProto.unbind = function () {
// the batcher's flush queue when its owner
// compiler has already been destroyed.
this.unbound = true
var i = this.instances.length
var i = this.dirs.length
while (i--) {
this.instances[i].unbind()
this.dirs[i].unbind()
}
i = this.deps.length
var subs
Expand Down
10 changes: 5 additions & 5 deletions src/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ CompilerProto.bindDirective = function (directive) {
compiler = compiler || this
binding = compiler.bindings[key] || compiler.createBinding(key)
}
binding.instances.push(directive)
binding.dirs.push(directive)
directive.binding = binding

// invoke bind hook if exists
Expand Down Expand Up @@ -672,7 +672,7 @@ CompilerProto.destroy = function () {
if (this.destroyed) return

var compiler = this,
i, key, dir, instances, binding,
i, key, dir, dirs, binding,
vm = compiler.vm,
el = compiler.el,
directives = compiler.dirs,
Expand All @@ -690,11 +690,11 @@ CompilerProto.destroy = function () {
dir = directives[i]
// if this directive is an instance of an external binding
// e.g. a directive that refers to a variable on the parent VM
// we need to remove it from that binding's instances
// we need to remove it from that binding's directives
// * empty and literal bindings do not have binding.
if (dir.binding && dir.binding.compiler !== compiler) {
instances = dir.binding.instances
if (instances) instances.splice(instances.indexOf(dir), 1)
dirs = dir.binding.dirs
if (dirs) dirs.splice(dirs.indexOf(dir), 1)
}
dir.unbind()
}
Expand Down
12 changes: 6 additions & 6 deletions test/unit/specs/binding.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ describe('UNIT: Binding', function () {
assert.ok(!b.root)
})

it('should have instances, subs and deps as Arrays', function () {
it('should have dirs, subs and deps as Arrays', function () {
var b = new Binding(null, 'test')
assert.ok(Array.isArray(b.instances), 'instances')
assert.ok(Array.isArray(b.dirs), 'dirs')
assert.ok(Array.isArray(b.subs), 'subs')
assert.ok(Array.isArray(b.deps), 'deps')
})
Expand All @@ -42,7 +42,7 @@ describe('UNIT: Binding', function () {
}
}
for (var i = 0; i < numInstances; i++) {
b.instances.push(instance)
b.dirs.push(instance)
}
b.pub = function () {
pubbed = true
Expand All @@ -59,7 +59,7 @@ describe('UNIT: Binding', function () {
assert.strictEqual(b.value, val)
})

it('should update the binding\'s instances', function () {
it('should update the binding\'s directives', function () {
assert.strictEqual(updated, val * numInstances)
})

Expand Down Expand Up @@ -140,7 +140,7 @@ describe('UNIT: Binding', function () {
}
}
for (var i = 0; i < numInstances; i++) {
b.instances.push(instance)
b.dirs.push(instance)
}

// mock deps
Expand All @@ -150,7 +150,7 @@ describe('UNIT: Binding', function () {

b.unbind()

it('should call unbind() of all instances', function () {
it('should call unbind() of all directives', function () {
assert.strictEqual(unbound, numInstances)
})

Expand Down
6 changes: 3 additions & 3 deletions test/unit/specs/viewmodel.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,13 +376,13 @@ describe('UNIT: ViewModel', function () {
var dirMock = {
binding: {
compiler: null,
instances: []
dirs: []
},
unbind: function () {
dirUnbindCalled = true
}
}
dirMock.binding.instances.push(dirMock)
dirMock.binding.dirs.push(dirMock)

var bindingsMock = {
test: {
Expand Down Expand Up @@ -474,7 +474,7 @@ describe('UNIT: ViewModel', function () {
})

it('should remove directives from external bindings', function () {
assert.strictEqual(dirMock.binding.instances.indexOf(dirMock), -1)
assert.strictEqual(dirMock.binding.dirs.indexOf(dirMock), -1)
})

it('should unbind all expressions', function () {
Expand Down

0 comments on commit 1a3ddfd

Please sign in to comment.