Quellcode durchsuchen

fix binding.unbind() removing wrong subs from dependency

Evan You vor 12 Jahren
Ursprung
Commit
6dc59855ab
3 geänderte Dateien mit 23 neuen und 6 gelöschten Zeilen
  1. 2 1
      src/binding.js
  2. 7 3
      src/compiler.js
  3. 14 2
      src/deps-parser.js

+ 2 - 1
src/binding.js

@@ -95,7 +95,8 @@ BindingProto.unbind = function () {
     var subs
     while (i--) {
         subs = this.deps[i].subs
-        subs.splice(subs.indexOf(this), 1)
+        var j = subs.indexOf(this)
+        if (j > -1) subs.splice(j, 1)
     }
 }
 

+ 7 - 3
src/compiler.js

@@ -909,7 +909,7 @@ CompilerProto.destroy = function () {
     if (this.destroyed) return
 
     var compiler = this,
-        i, key, dir, dirs, binding,
+        i, j, key, dir, dirs, binding,
         vm          = compiler.vm,
         el          = compiler.el,
         directives  = compiler.dirs,
@@ -933,7 +933,10 @@ CompilerProto.destroy = function () {
         // * empty and literal bindings do not have binding.
         if (dir.binding && dir.binding.compiler !== compiler) {
             dirs = dir.binding.dirs
-            if (dirs) dirs.splice(dirs.indexOf(dir), 1)
+            if (dirs) {
+                j = dirs.indexOf(dir)
+                if (j > -1) dirs.splice(j, 1)
+            }
         }
         dir.unbind()
     }
@@ -960,7 +963,8 @@ CompilerProto.destroy = function () {
 
     // remove self from parent
     if (parent) {
-        parent.children.splice(parent.children.indexOf(compiler), 1)
+        j = parent.children.indexOf(compiler)
+        if (j > -1) parent.children.splice(j, 1)
     }
 
     // finally remove dom element

+ 14 - 2
src/deps-parser.js

@@ -18,8 +18,8 @@ function catchDeps (binding) {
             // avoid duplicate bindings
             (has && has.compiler === dep.compiler) ||
             // avoid repeated items as dependency
-            // since all inside changes trigger array change too
-            (dep.compiler.repeat && dep.compiler.parent === binding.compiler)
+            // only when the binding is from self or the parent chain
+            (dep.compiler.repeat && !isParentOf(dep.compiler, binding.compiler))
         ) {
             return
         }
@@ -32,6 +32,18 @@ function catchDeps (binding) {
     catcher.off('get')
 }
 
+/**
+ *  Test if A is a parent of or equals B
+ */
+function isParentOf (a, b) {
+    while (b) {
+        if (a === b) {
+            return true
+        }
+        b = b.parent
+    }
+}
+
 module.exports = {
 
     /**