Skip to content

Commit 6204145

Browse files
committed
fix attach/ready hooks (fix vuejs#1005)
1 parent c1bb44a commit 6204145

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

src/api/lifecycle.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,11 @@ exports.$mount = function (el) {
2323
this._compile(el)
2424
this._isCompiled = true
2525
this._callHook('compiled')
26+
this._initDOMHooks()
2627
if (_.inDoc(this.$el)) {
2728
this._callHook('attached')
28-
this._initDOMHooks()
2929
ready.call(this)
3030
} else {
31-
this._initDOMHooks()
3231
this.$once('hook:attached', ready)
3332
}
3433
return this

src/instance/events.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,10 @@ exports._initDOMHooks = function () {
8181
*/
8282

8383
function onAttached () {
84-
this._isAttached = true
85-
this.$children.forEach(callAttach)
84+
if (!this._isAttached) {
85+
this._isAttached = true
86+
this.$children.forEach(callAttach)
87+
}
8688
}
8789

8890
/**
@@ -102,8 +104,10 @@ function callAttach (child) {
102104
*/
103105

104106
function onDetached () {
105-
this._isAttached = false
106-
this.$children.forEach(callDetach)
107+
if (this._isAttached) {
108+
this._isAttached = false
109+
this.$children.forEach(callDetach)
110+
}
107111
}
108112

109113
/**

test/unit/specs/misc_spec.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,38 @@ describe('Misc', function () {
8080
expect(el.innerHTML.replace(xmlns, '')).toBe('<svg><text>1</text><text>2</text><text>3</text></svg>')
8181
})
8282

83+
// #1005
84+
it('call attach/ready/detach hook for child components', function () {
85+
Vue.options.replace = true
86+
var el = document.createElement('div')
87+
var logs = []
88+
function log (n) {
89+
return function () {
90+
logs.push(n)
91+
}
92+
}
93+
document.body.appendChild(el)
94+
var vm = new Vue({
95+
el: el,
96+
attached: log(0),
97+
ready: log(1),
98+
detached: log(2),
99+
template: '<div><test></test><test></test></div>',
100+
components: {
101+
test: {
102+
template: '<span>hi</span>',
103+
attached: log(3),
104+
ready: log(4),
105+
detached: log(5)
106+
}
107+
}
108+
})
109+
expect(vm.$el.innerHTML).toBe('<span>hi</span><span>hi</span>')
110+
expect(logs.join()).toBe('0,3,4,3,4,1')
111+
logs = []
112+
vm.$destroy(true)
113+
expect(logs.join()).toBe('2,5,5')
114+
Vue.options.replace = false
115+
})
116+
83117
})

0 commit comments

Comments
 (0)