Skip to content

fix the wrong orders of call 'detached' and 'destroyed' hook - #3870 #4888

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 1.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions src/fragment/fragment.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ function multiRemove () {
}
self.destroy()
})
this.afterRemove()
}

/**
Expand All @@ -157,6 +158,22 @@ Fragment.prototype.beforeRemove = function () {
// fragments, depth-first
this.childFrags[i].beforeRemove(false)
}

var dirs = this.unlink.dirs
for (i = 0, l = dirs.length; i < l; i++) {
// disable the watchers on all the directives
// so that the rendered content stays the same
// during removal.
dirs[i]._watcher && dirs[i]._watcher.teardown()
}
}

/**
* ensure destroyed after detached.
*/

Fragment.prototype.afterRemove = function () {
var i, l
for (i = 0, l = this.children.length; i < l; i++) {
// Call destroy for all contained instances,
// with remove:false and defer:true.
Expand All @@ -165,13 +182,6 @@ Fragment.prototype.beforeRemove = function () {
// on them.
this.children[i].$destroy(false, true)
}
var dirs = this.unlink.dirs
for (i = 0, l = dirs.length; i < l; i++) {
// disable the watchers on all the directives
// so that the rendered content stays the same
// during removal.
dirs[i]._watcher && dirs[i]._watcher.teardown()
}
}

/**
Expand Down
42 changes: 42 additions & 0 deletions test/unit/specs/instance/events_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,48 @@ describe('Instance Events', function () {
expect(spy2.calls.count()).toBe(2)
})

// Github issues #3870
it('detached then destroyed', function (done) {
var el = document.createElement('div')
document.body.appendChild(el) // for inDoc.
var vm = new Vue({
el: el,
template: '<div><mod v-if="show"></mod></div>',
data: {
show: false
},
components: {
mod: {
template: '<div><comp></comp></div>',
components: {
comp: {
template: '<div>123</div>',
detached: function () {
spy()
expect(spy2).not.toHaveBeenCalled()
},
destroyed: function () {
spy2()
expect(spy).toHaveBeenCalled()
}
}
}
}
}
})
_.nextTick(function () {
vm.show = true
_.nextTick(function () {
vm.show = false
_.nextTick(function () {
expect(spy).toHaveBeenCalled()
expect(spy2).toHaveBeenCalled()
done()
})
})
})
})

it('should not fire on detached child', function () {
var el = document.createElement('div')
var childEl = document.createElement('div')
Expand Down