Skip to content

Commit 4339243

Browse files
committed
fix #561 attach/detach/ready hooks for children in partial compile blocks
1 parent e9816ab commit 4339243

File tree

3 files changed

+52
-33
lines changed

3 files changed

+52
-33
lines changed

src/directives/if.js

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,43 @@ module.exports = {
4545
insert: function () {
4646
// avoid duplicate inserts, since update() can be
4747
// called with different truthy values
48-
if (this.decompile) {
49-
return
48+
if (!this.unlink) {
49+
this.compile(this.template)
5050
}
51+
},
52+
53+
compile: function (template) {
5154
var vm = this.vm
52-
var frag = templateParser.clone(this.template)
53-
var decompile = this.linker(vm, frag)
54-
this.decompile = function () {
55-
decompile()
56-
transition.blockRemove(this.start, this.end, vm)
57-
}
55+
var frag = templateParser.clone(template)
56+
var originalChildLength = vm._children
57+
? vm._children.length
58+
: 0
59+
this.unlink = this.linker
60+
? this.linker(vm, frag)
61+
: vm.$compile(frag)
5862
transition.blockAppend(frag, this.end, vm)
63+
this.children = vm._children
64+
? vm._children.slice(originalChildLength)
65+
: null
66+
if (this.children && _.inDoc(vm.$el)) {
67+
this.children.forEach(function (child) {
68+
child._callHook('attached')
69+
})
70+
}
5971
},
6072

6173
teardown: function () {
62-
if (this.decompile) {
63-
this.decompile()
64-
this.decompile = null
74+
if (!this.unlink) return
75+
transition.blockRemove(this.start, this.end, this.vm)
76+
if (this.children && _.inDoc(this.vm.$el)) {
77+
this.children.forEach(function (child) {
78+
if (!child._isDestroyed) {
79+
child._callHook('detached')
80+
}
81+
})
6582
}
83+
this.unlink()
84+
this.unlink = null
6685
}
6786

6887
}

src/directives/partial.js

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
var _ = require('../util')
22
var templateParser = require('../parse/template')
3-
var transition = require('../transition')
3+
var vIf = require('./if')
44

55
module.exports = {
66

77
isLiteral: true,
88

9+
// same logic reuse from v-if
10+
compile: vIf.compile,
11+
teardown: vIf.teardown,
12+
913
bind: function () {
1014
var el = this.el
1115
this.start = document.createComment('v-partial-start')
@@ -20,35 +24,20 @@ module.exports = {
2024
}
2125
_.before(this.start, this.end)
2226
if (!this._isDynamicLiteral) {
23-
this.compile(this.expression)
27+
this.insert(this.expression)
2428
}
2529
},
2630

2731
update: function (id) {
2832
this.teardown()
29-
this.compile(id)
33+
this.insert(id)
3034
},
3135

32-
compile: function (id) {
36+
insert: function (id) {
3337
var partial = this.vm.$options.partials[id]
3438
_.assertAsset(partial, 'partial', id)
35-
if (!partial) {
36-
return
37-
}
38-
var vm = this.vm
39-
var frag = templateParser.parse(partial, true)
40-
var decompile = vm.$compile(frag)
41-
this.decompile = function () {
42-
decompile()
43-
transition.blockRemove(this.start, this.end, vm)
44-
}
45-
transition.blockAppend(frag, this.end, vm)
46-
},
47-
48-
teardown: function () {
49-
if (this.decompile) {
50-
this.decompile()
51-
this.decompile = null
39+
if (partial) {
40+
this.compile(templateParser.parse(partial))
5241
}
5342
}
5443

test/unit/specs/directives/if_spec.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ if (_.inBrowser) {
7070
})
7171

7272
it('v-if + v-component', function (done) {
73+
var attachSpy = jasmine.createSpy()
74+
var detachSpy = jasmine.createSpy()
75+
var readySpy = jasmine.createSpy()
7376
var vm = new Vue({
7477
el: el,
7578
data: { ok: false },
@@ -79,20 +82,28 @@ if (_.inBrowser) {
7982
data: function () {
8083
return { a: 123 }
8184
},
82-
template: '{{a}}'
85+
template: '{{a}}',
86+
ready: readySpy,
87+
attached: attachSpy,
88+
detached: detachSpy
8389
}
8490
}
8591
})
92+
vm.$appendTo(document.body)
8693
expect(el.innerHTML).toBe(wrap(''))
8794
expect(vm._children).toBeNull()
8895
vm.ok = true
8996
_.nextTick(function () {
9097
expect(el.innerHTML).toBe(wrap('<div>123</div><!--v-component-->'))
9198
expect(vm._children.length).toBe(1)
99+
expect(attachSpy).toHaveBeenCalled()
100+
expect(readySpy).toHaveBeenCalled()
92101
vm.ok = false
93102
_.nextTick(function () {
103+
expect(detachSpy).toHaveBeenCalled()
94104
expect(el.innerHTML).toBe(wrap(''))
95105
expect(vm._children.length).toBe(0)
106+
vm.$remove()
96107
done()
97108
})
98109
})

0 commit comments

Comments
 (0)