call attach/detach hooks for transcluded components inside v-if (fix #684)

This commit is contained in:
Evan You 2015-04-18 01:05:20 -04:00
parent 1df5eb64a5
commit 3a9bfff268
2 changed files with 38 additions and 0 deletions

View File

@ -53,6 +53,14 @@ module.exports = {
;(this.contentPositions = this.contentPositions || []).push(i)
}
}
// keep track of any transcluded components contained within
// the conditional block. we need to call attach/detach hooks
// for them.
this.transCpnts =
this.vm._transCpnts &&
this.vm._transCpnts.filter(function (c) {
return el.contains(c.$el)
})
},
update: function (value) {
@ -87,6 +95,9 @@ module.exports = {
: vm.$compile(frag)
transition.blockAppend(frag, this.end, vm)
this.children = vm._children.slice(originalChildLength)
if (this.transCpnts) {
this.children = this.children.concat(this.transCpnts)
}
if (this.children.length && _.inDoc(vm.$el)) {
this.children.forEach(function (child) {
child._callHook('attached')

View File

@ -212,5 +212,32 @@ if (_.inBrowser) {
})
})
it('call attach/detach for transcluded components', function (done) {
document.body.appendChild(el)
var attachSpy = jasmine.createSpy('attached')
var detachSpy = jasmine.createSpy('detached')
var vm = new Vue({
el: el,
data: { show: true },
template: '<div v-component="outer"><div v-component="transcluded"></div></div>',
components: {
outer: {
template: '<div v-if="$parent.show"><content></content></div>'
},
transcluded: {
template: 'transcluded',
attached: attachSpy,
detached: detachSpy
}
}
})
expect(attachSpy).toHaveBeenCalled()
vm.show = false
_.nextTick(function () {
expect(detachSpy).toHaveBeenCalled()
done()
})
})
})
}