Jelajahi Sumber

optimize destroy unlink

Evan You 11 tahun lalu
induk
melakukan
db12331e03
3 mengubah file dengan 17 tambahan dan 10 penghapusan
  1. 13 9
      src/compiler/compile.js
  2. 3 1
      src/instance/compile.js
  3. 1 0
      src/observer/array.js

+ 13 - 9
src/compiler/compile.js

@@ -60,18 +60,19 @@ function compile (el, options, partial, transcluded) {
     if (nodeLinkFn) nodeLinkFn(source, el, host)
     if (childLinkFn) childLinkFn(source, childNodes, host)
 
+    var selfDirs = vm._directives.slice(originalDirCount)
+    var parentDirs = vm.$parent &&
+      vm.$parent._directives.slice(parentOriginalDirCount)
+
     /**
      * The linker function returns an unlink function that
      * tearsdown all directives instances generated during
      * the process.
+     *
+     * @param {Boolean} destroying
      */
-
-    var selfDirs = vm._directives.slice(originalDirCount)
-    var parentDirs = vm.$parent &&
-      vm.$parent._directives.slice(parentOriginalDirCount)
-
-    return function unlink () {
-      teardownDirs(vm, selfDirs)
+    return function unlink (destroying) {
+      teardownDirs(vm, selfDirs, destroying)
       if (parentDirs) {
         teardownDirs(vm.$parent, parentDirs)
       }
@@ -92,13 +93,16 @@ function compile (el, options, partial, transcluded) {
  *
  * @param {Vue} vm
  * @param {Array} dirs
+ * @param {Boolean} destroying
  */
 
-function teardownDirs (vm, dirs) {
+function teardownDirs (vm, dirs, destroying) {
   var i = dirs.length
   while (i--) {
     dirs[i]._teardown()
-    vm._directives.$remove(dirs[i])
+    if (!destroying) {
+      vm._directives.$remove(dirs[i])
+    }
   }
 }
 

+ 3 - 1
src/instance/compile.js

@@ -110,7 +110,9 @@ exports._destroy = function (remove, deferCleanup) {
   // teardown all directives. this also tearsdown all
   // directive-owned watchers.
   if (this._unlinkFn) {
-    this._unlinkFn()
+    // passing destroying: true to avoid searching and
+    // splicing the directives
+    this._unlinkFn(true)
   }
   // teardown all user watchers.
   var watcher

+ 1 - 0
src/observer/array.js

@@ -78,6 +78,7 @@ _.define(
   arrayProto,
   '$remove',
   function $remove (index) {
+    /* istanbul ignore if */
     if (!this.length) return
     if (typeof index !== 'number') {
       index = this.indexOf(index)