Skip to content

Commit 3a9bfff

Browse files
committed
call attach/detach hooks for transcluded components inside v-if (fix vuejs#684)
1 parent 1df5eb6 commit 3a9bfff

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/directives/if.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ module.exports = {
5353
;(this.contentPositions = this.contentPositions || []).push(i)
5454
}
5555
}
56+
// keep track of any transcluded components contained within
57+
// the conditional block. we need to call attach/detach hooks
58+
// for them.
59+
this.transCpnts =
60+
this.vm._transCpnts &&
61+
this.vm._transCpnts.filter(function (c) {
62+
return el.contains(c.$el)
63+
})
5664
},
5765

5866
update: function (value) {
@@ -87,6 +95,9 @@ module.exports = {
8795
: vm.$compile(frag)
8896
transition.blockAppend(frag, this.end, vm)
8997
this.children = vm._children.slice(originalChildLength)
98+
if (this.transCpnts) {
99+
this.children = this.children.concat(this.transCpnts)
100+
}
90101
if (this.children.length && _.inDoc(vm.$el)) {
91102
this.children.forEach(function (child) {
92103
child._callHook('attached')

test/unit/specs/directives/if_spec.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,5 +212,32 @@ if (_.inBrowser) {
212212
})
213213
})
214214

215+
it('call attach/detach for transcluded components', function (done) {
216+
document.body.appendChild(el)
217+
var attachSpy = jasmine.createSpy('attached')
218+
var detachSpy = jasmine.createSpy('detached')
219+
var vm = new Vue({
220+
el: el,
221+
data: { show: true },
222+
template: '<div v-component="outer"><div v-component="transcluded"></div></div>',
223+
components: {
224+
outer: {
225+
template: '<div v-if="$parent.show"><content></content></div>'
226+
},
227+
transcluded: {
228+
template: 'transcluded',
229+
attached: attachSpy,
230+
detached: detachSpy
231+
}
232+
}
233+
})
234+
expect(attachSpy).toHaveBeenCalled()
235+
vm.show = false
236+
_.nextTick(function () {
237+
expect(detachSpy).toHaveBeenCalled()
238+
done()
239+
})
240+
})
241+
215242
})
216243
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy