Skip to content

Commit db12331

Browse files
committed
optimize destroy unlink
1 parent 3d5b934 commit db12331

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

src/compiler/compile.js

+13-9
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,19 @@ function compile (el, options, partial, transcluded) {
6060
if (nodeLinkFn) nodeLinkFn(source, el, host)
6161
if (childLinkFn) childLinkFn(source, childNodes, host)
6262

63+
var selfDirs = vm._directives.slice(originalDirCount)
64+
var parentDirs = vm.$parent &&
65+
vm.$parent._directives.slice(parentOriginalDirCount)
66+
6367
/**
6468
* The linker function returns an unlink function that
6569
* tearsdown all directives instances generated during
6670
* the process.
71+
*
72+
* @param {Boolean} destroying
6773
*/
68-
69-
var selfDirs = vm._directives.slice(originalDirCount)
70-
var parentDirs = vm.$parent &&
71-
vm.$parent._directives.slice(parentOriginalDirCount)
72-
73-
return function unlink () {
74-
teardownDirs(vm, selfDirs)
74+
return function unlink (destroying) {
75+
teardownDirs(vm, selfDirs, destroying)
7576
if (parentDirs) {
7677
teardownDirs(vm.$parent, parentDirs)
7778
}
@@ -92,13 +93,16 @@ function compile (el, options, partial, transcluded) {
9293
*
9394
* @param {Vue} vm
9495
* @param {Array} dirs
96+
* @param {Boolean} destroying
9597
*/
9698

97-
function teardownDirs (vm, dirs) {
99+
function teardownDirs (vm, dirs, destroying) {
98100
var i = dirs.length
99101
while (i--) {
100102
dirs[i]._teardown()
101-
vm._directives.$remove(dirs[i])
103+
if (!destroying) {
104+
vm._directives.$remove(dirs[i])
105+
}
102106
}
103107
}
104108

src/instance/compile.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,9 @@ exports._destroy = function (remove, deferCleanup) {
110110
// teardown all directives. this also tearsdown all
111111
// directive-owned watchers.
112112
if (this._unlinkFn) {
113-
this._unlinkFn()
113+
// passing destroying: true to avoid searching and
114+
// splicing the directives
115+
this._unlinkFn(true)
114116
}
115117
// teardown all user watchers.
116118
var watcher

src/observer/array.js

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ _.define(
7878
arrayProto,
7979
'$remove',
8080
function $remove (index) {
81+
/* istanbul ignore if */
8182
if (!this.length) return
8283
if (typeof index !== 'number') {
8384
index = this.indexOf(index)

0 commit comments

Comments
 (0)