Skip to content

Commit 95c1c16

Browse files
author
Evan You
committed
destroy children
1 parent 0893163 commit 95c1c16

File tree

5 files changed

+47
-22
lines changed

5 files changed

+47
-22
lines changed

src/compiler.js

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,12 @@ var Emitter = require('./emitter'),
3030
function Compiler (vm, options) {
3131

3232
var compiler = this
33-
// indicate that we are intiating this instance
34-
// so we should not run any transitions
35-
compiler.init = true
33+
34+
// default state
35+
compiler.init = true
36+
compiler.repeat = false
37+
compiler.destroyed = false
38+
compiler.delayReady = false
3639

3740
// process and extend options
3841
options = compiler.options = options || makeHash()
@@ -55,7 +58,7 @@ function Compiler (vm, options) {
5558
compiler.deferred = []
5659
compiler.exps = []
5760
compiler.computed = []
58-
compiler.childCompilers = []
61+
compiler.children = []
5962
compiler.emitter = new Emitter()
6063
compiler.emitter._ctx = vm
6164
compiler.delegators = makeHash()
@@ -71,7 +74,7 @@ function Compiler (vm, options) {
7174
childId = utils.attr(el, 'ref')
7275
if (parentVM) {
7376
compiler.parent = parentVM.$compiler
74-
parentVM.$compiler.childCompilers.push(compiler)
77+
parentVM.$compiler.children.push(compiler)
7578
def(vm, '$parent', parentVM)
7679
if (childId) {
7780
compiler.childId = childId
@@ -254,7 +257,7 @@ CompilerProto.setupObserver = function () {
254257
}
255258

256259
function broadcast (event) {
257-
var children = compiler.childCompilers
260+
var children = compiler.children
258261
if (children) {
259262
var child, i = children.length
260263
while (i--) {
@@ -806,7 +809,9 @@ CompilerProto.destroy = function () {
806809
directives = compiler.dirs,
807810
exps = compiler.exps,
808811
bindings = compiler.bindings,
809-
delegators = compiler.delegators
812+
delegators = compiler.delegators,
813+
children = compiler.children,
814+
parent = compiler.parent
810815

811816
compiler.execHook('beforeDestroy')
812817

@@ -847,13 +852,17 @@ CompilerProto.destroy = function () {
847852
el.removeEventListener(key, delegators[key].handler)
848853
}
849854

855+
// destroy all children
856+
i = children.length
857+
while (i--) {
858+
children[i].destroy()
859+
}
860+
850861
// remove self from parent
851-
var parent = compiler.parent,
852-
childId = compiler.childId
853862
if (parent) {
854-
parent.childCompilers.splice(parent.childCompilers.indexOf(compiler), 1)
855-
if (childId) {
856-
delete parent.vm.$[childId]
863+
parent.children.splice(parent.children.indexOf(compiler), 1)
864+
if (compiler.childId) {
865+
delete parent.vm.$[compiler.childId]
857866
}
858867
}
859868

src/emitter.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
function Emitter () {}
1+
function Emitter () {
2+
this._ctx = this
3+
}
24

35
var EmitterProto = Emitter.prototype,
46
slice = [].slice
@@ -63,7 +65,7 @@ Emitter.prototype.emit = function(event){
6365
if (callbacks) {
6466
callbacks = callbacks.slice(0)
6567
for (var i = 0, len = callbacks.length; i < len; i++) {
66-
callbacks[i].apply(this._ctx || this, args)
68+
callbacks[i].apply(this._ctx, args)
6769
}
6870
}
6971

src/viewmodel.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def(VMProto, '$destroy', function () {
8383
* broadcast an event to all child VMs recursively.
8484
*/
8585
def(VMProto, '$broadcast', function () {
86-
var children = this.$compiler.childCompilers,
86+
var children = this.$compiler.children,
8787
i = children.length,
8888
child
8989
while (i--) {

test/unit/specs/api.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -635,22 +635,28 @@ describe('UNIT: API', function () {
635635
})
636636

637637
describe('parent', function () {
638+
639+
var parent, child
638640

639-
it('should create parent-child relation between VMs', function () {
640-
641-
var parent = new Vue({
641+
it('should allow child to access parent bindings', function () {
642+
643+
parent = new Vue({
642644
data: {
643645
test: 'from parent'
644646
}
645647
})
646648

647-
var child = new Vue({
649+
child = new Vue({
648650
parent: parent,
649651
template: '{{test}}'
650652
})
651653

652654
assert.strictEqual(child.$el.textContent, 'from parent')
653655

656+
})
657+
658+
it('should allow event communication between parent and child', function () {
659+
654660
var dispatched = false,
655661
broadcasted = false
656662
parent.$on('dispatch', function () {
@@ -667,6 +673,13 @@ describe('UNIT: API', function () {
667673

668674
})
669675

676+
it('should destroy the child when parent is destroyed', function () {
677+
678+
parent.$destroy()
679+
assert.ok(child.$compiler.destroyed)
680+
681+
})
682+
670683
})
671684

672685
describe('directives', function () {

test/unit/specs/viewmodel.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -457,8 +457,9 @@ describe('UNIT: ViewModel', function () {
457457
}],
458458
bindings: bindingsMock,
459459
childId: 'test',
460+
children: [],
460461
parent: {
461-
childCompilers: [],
462+
children: [],
462463
vm: {
463464
$: {
464465
'test': true
@@ -487,7 +488,7 @@ describe('UNIT: ViewModel', function () {
487488
}
488489
}
489490

490-
compilerMock.parent.childCompilers.push(compilerMock)
491+
compilerMock.parent.children.push(compilerMock)
491492

492493
destroy.call(compilerMock)
493494

@@ -523,7 +524,7 @@ describe('UNIT: ViewModel', function () {
523524

524525
it('should remove self from parent', function () {
525526
var parent = compilerMock.parent
526-
assert.ok(parent.childCompilers.indexOf(compilerMock), -1)
527+
assert.ok(parent.children.indexOf(compilerMock), -1)
527528
assert.strictEqual(parent.vm.$[compilerMock.childId], undefined)
528529
})
529530

0 commit comments

Comments
 (0)