Skip to content

Commit 1a3ddfd

Browse files
committed
binding check dir/subs count before queueing, rename instances -> dirs
1 parent 48e328c commit 1a3ddfd

File tree

4 files changed

+23
-21
lines changed

4 files changed

+23
-21
lines changed

src/binding.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function Binding (compiler, key, isExp, isFn) {
1616
this.root = !this.isExp && key.indexOf('.') === -1
1717
this.compiler = compiler
1818
this.key = key
19-
this.instances = []
19+
this.dirs = []
2020
this.subs = []
2121
this.deps = []
2222
this.unbound = false
@@ -31,17 +31,19 @@ BindingProto.update = function (value) {
3131
if (!this.isComputed || this.isFn) {
3232
this.value = value
3333
}
34-
batcher.queue(this)
34+
if (this.dirs.length || this.subs.length) {
35+
batcher.queue(this)
36+
}
3537
}
3638

3739
/**
38-
* Actually update the instances.
40+
* Actually update the directives.
3941
*/
4042
BindingProto._update = function () {
41-
var i = this.instances.length,
43+
var i = this.dirs.length,
4244
value = this.val()
4345
while (i--) {
44-
this.instances[i].update(value)
46+
this.dirs[i].update(value)
4547
}
4648
this.pub()
4749
}
@@ -76,9 +78,9 @@ BindingProto.unbind = function () {
7678
// the batcher's flush queue when its owner
7779
// compiler has already been destroyed.
7880
this.unbound = true
79-
var i = this.instances.length
81+
var i = this.dirs.length
8082
while (i--) {
81-
this.instances[i].unbind()
83+
this.dirs[i].unbind()
8284
}
8385
i = this.deps.length
8486
var subs

src/compiler.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ CompilerProto.bindDirective = function (directive) {
490490
compiler = compiler || this
491491
binding = compiler.bindings[key] || compiler.createBinding(key)
492492
}
493-
binding.instances.push(directive)
493+
binding.dirs.push(directive)
494494
directive.binding = binding
495495

496496
// invoke bind hook if exists
@@ -672,7 +672,7 @@ CompilerProto.destroy = function () {
672672
if (this.destroyed) return
673673

674674
var compiler = this,
675-
i, key, dir, instances, binding,
675+
i, key, dir, dirs, binding,
676676
vm = compiler.vm,
677677
el = compiler.el,
678678
directives = compiler.dirs,
@@ -690,11 +690,11 @@ CompilerProto.destroy = function () {
690690
dir = directives[i]
691691
// if this directive is an instance of an external binding
692692
// e.g. a directive that refers to a variable on the parent VM
693-
// we need to remove it from that binding's instances
693+
// we need to remove it from that binding's directives
694694
// * empty and literal bindings do not have binding.
695695
if (dir.binding && dir.binding.compiler !== compiler) {
696-
instances = dir.binding.instances
697-
if (instances) instances.splice(instances.indexOf(dir), 1)
696+
dirs = dir.binding.dirs
697+
if (dirs) dirs.splice(dirs.indexOf(dir), 1)
698698
}
699699
dir.unbind()
700700
}

test/unit/specs/binding.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ describe('UNIT: Binding', function () {
2020
assert.ok(!b.root)
2121
})
2222

23-
it('should have instances, subs and deps as Arrays', function () {
23+
it('should have dirs, subs and deps as Arrays', function () {
2424
var b = new Binding(null, 'test')
25-
assert.ok(Array.isArray(b.instances), 'instances')
25+
assert.ok(Array.isArray(b.dirs), 'dirs')
2626
assert.ok(Array.isArray(b.subs), 'subs')
2727
assert.ok(Array.isArray(b.deps), 'deps')
2828
})
@@ -42,7 +42,7 @@ describe('UNIT: Binding', function () {
4242
}
4343
}
4444
for (var i = 0; i < numInstances; i++) {
45-
b.instances.push(instance)
45+
b.dirs.push(instance)
4646
}
4747
b.pub = function () {
4848
pubbed = true
@@ -59,7 +59,7 @@ describe('UNIT: Binding', function () {
5959
assert.strictEqual(b.value, val)
6060
})
6161

62-
it('should update the binding\'s instances', function () {
62+
it('should update the binding\'s directives', function () {
6363
assert.strictEqual(updated, val * numInstances)
6464
})
6565

@@ -140,7 +140,7 @@ describe('UNIT: Binding', function () {
140140
}
141141
}
142142
for (var i = 0; i < numInstances; i++) {
143-
b.instances.push(instance)
143+
b.dirs.push(instance)
144144
}
145145

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

151151
b.unbind()
152152

153-
it('should call unbind() of all instances', function () {
153+
it('should call unbind() of all directives', function () {
154154
assert.strictEqual(unbound, numInstances)
155155
})
156156

test/unit/specs/viewmodel.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -376,13 +376,13 @@ describe('UNIT: ViewModel', function () {
376376
var dirMock = {
377377
binding: {
378378
compiler: null,
379-
instances: []
379+
dirs: []
380380
},
381381
unbind: function () {
382382
dirUnbindCalled = true
383383
}
384384
}
385-
dirMock.binding.instances.push(dirMock)
385+
dirMock.binding.dirs.push(dirMock)
386386

387387
var bindingsMock = {
388388
test: {
@@ -474,7 +474,7 @@ describe('UNIT: ViewModel', function () {
474474
})
475475

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

480480
it('should unbind all expressions', function () {

0 commit comments

Comments
 (0)