Skip to content

Commit 635fada

Browse files
committed
unlink transcluded content on destroy
1 parent 1c476ae commit 635fada

File tree

3 files changed

+34
-33
lines changed

3 files changed

+34
-33
lines changed

src/compiler/compile.js

+28-22
Original file line numberDiff line numberDiff line change
@@ -61,31 +61,19 @@ function compile (el, options, partial, transcluded) {
6161
if (childLinkFn) childLinkFn(source, childNodes, host)
6262

6363
/**
64-
* If this is a partial compile, the linker function
65-
* returns an unlink function that tearsdown all
66-
* directives instances generated during the partial
67-
* linking.
64+
* The linker function returns an unlink function that
65+
* tearsdown all directives instances generated during
66+
* the process.
6867
*/
6968

70-
if (partial && !transcluded) {
71-
var selfDirs = vm._directives.slice(originalDirCount)
72-
var parentDirs = vm.$parent &&
73-
vm.$parent._directives.slice(parentOriginalDirCount)
69+
var selfDirs = vm._directives.slice(originalDirCount)
70+
var parentDirs = vm.$parent &&
71+
vm.$parent._directives.slice(parentOriginalDirCount)
7472

75-
var teardownDirs = function (vm, dirs) {
76-
var i = dirs.length
77-
while (i--) {
78-
dirs[i]._teardown()
79-
}
80-
i = vm._directives.indexOf(dirs[0])
81-
vm._directives.splice(i, dirs.length)
82-
}
83-
84-
return function unlink () {
85-
teardownDirs(vm, selfDirs)
86-
if (parentDirs) {
87-
teardownDirs(vm.$parent, parentDirs)
88-
}
73+
return function unlink () {
74+
teardownDirs(vm, selfDirs)
75+
if (parentDirs) {
76+
teardownDirs(vm.$parent, parentDirs)
8977
}
9078
}
9179
}
@@ -99,6 +87,24 @@ function compile (el, options, partial, transcluded) {
9987
return compositeLinkFn
10088
}
10189

90+
/**
91+
* Teardown a subset of directives on a vm.
92+
*
93+
* @param {Vue} vm
94+
* @param {Array} dirs
95+
*/
96+
97+
function teardownDirs (vm, dirs) {
98+
var i = dirs.length
99+
while (i--) {
100+
dirs[i]._teardown()
101+
}
102+
i = vm._directives.indexOf(dirs[0])
103+
if (i > -1) {
104+
vm._directives.splice(i, dirs.length)
105+
}
106+
}
107+
102108
/**
103109
* Compile the root element of a component. There are
104110
* 4 types of things to process here:

src/instance/compile.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ exports._compile = function (el) {
2121
if (options._linkFn) {
2222
// pre-transcluded with linker, just use it
2323
this._initElement(el)
24-
options._linkFn(this, el)
24+
this._unlinkFn = options._linkFn(this, el)
2525
} else {
2626
// transclude and init element
2727
// transclude can potentially replace original
@@ -30,7 +30,7 @@ exports._compile = function (el) {
3030
el = transclude(el, options)
3131
this._initElement(el)
3232
// compile and link the rest
33-
compile(el, options)(this, el)
33+
this._unlinkFn = compile(el, options)(this, el)
3434
// finally replace original
3535
if (options.replace) {
3636
_.replace(original, el)
@@ -110,11 +110,9 @@ exports._destroy = function (remove, deferCleanup) {
110110
this._children[i].$destroy()
111111
}
112112
// teardown all directives. this also tearsdown all
113-
// directive-owned watchers. intentionally check for
114-
// directives array length on every loop since directives
115-
// that manages partial compilation can splice ones out
116-
for (i = 0; i < this._directives.length; i++) {
117-
this._directives[i]._teardown()
113+
// directive-owned watchers.
114+
if (this._unlinkFn) {
115+
this._unlinkFn()
118116
}
119117
// teardown all user watchers.
120118
var watcher

src/instance/init.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,12 @@ exports._init = function (options) {
4444
this._isReady =
4545
this._isAttached =
4646
this._isBeingDestroyed = false
47+
this._unlinkFn = null
4748

4849
// children
4950
this._children = []
5051
this._childCtors = {}
5152

52-
// transclusion unlink functions
53-
this._containerUnlinkFn =
54-
this._contentUnlinkFn = null
55-
5653
// transcluded components that belong to the parent.
5754
// need to keep track of them so that we can call
5855
// attached/detached hooks on them.

0 commit comments

Comments
 (0)