Browse Source

really fix GC

Evan You 13 years ago
parent
commit
a85453cc89
4 changed files with 23 additions and 15 deletions
  1. 5 1
      src/binding.js
  2. 1 1
      src/compiler.js
  3. 1 2
      src/directives/each.js
  4. 16 11
      src/utils.js

+ 5 - 1
src/binding.js

@@ -129,6 +129,9 @@ BindingProto.refresh = function () {
     }
 }
 
+/*
+ *  Unbind the binding, remove itself from all of its dependencies
+ */
 BindingProto.unbind = function () {
     var i = this.instances.length
     while (i--) {
@@ -141,7 +144,8 @@ BindingProto.unbind = function () {
         subs.splice(subs.indexOf(this), 1)
     }
     if (Array.isArray(this.value)) this.value.off('mutate')
-    this.vm = this.compiler = this.pubs = this.subs = this.instances = null
+    this.vm = this.compiler = this.pubs =
+    this.subs = this.instances = this.deps = null
 }
 
 /*

+ 1 - 1
src/compiler.js

@@ -289,7 +289,7 @@ CompilerProto.destroy = function () {
         dir = this.directives[i]
         if (dir.binding.compiler !== this) {
             inss = dir.binding.instances
-            inss.splice(inss.indexOf(dir), 1)
+            if (inss) inss.splice(inss.indexOf(dir), 1)
         }
         dir.unbind()
     }

+ 1 - 2
src/directives/each.js

@@ -1,5 +1,4 @@
-var config = require('../config'),
-    utils  = require('../utils')
+var config = require('../config')
 
 /*
  *  Mathods that perform precise DOM manipulation

+ 16 - 11
src/utils.js

@@ -82,17 +82,22 @@ module.exports = {
      */
     watchArray: function (collection) {
         Emitter(collection)
-        arrayMutators.forEach(function (method) {
-            collection[method] = function () {
-                var result = aproto[method].apply(this, arguments)
-                collection.emit('mutate', {
-                    method: method,
-                    args: aproto.slice.call(arguments),
-                    result: result
-                })
-            }
-        })
-        for (var method in arrayAugmentations) {
+        var method, i = arrayMutators.length
+        while (i--) {
+            method = arrayMutators[i]
+            /* jshint loopfunc: true */
+            collection[method] = (function (method) {
+                return function () {
+                    var result = aproto[method].apply(this, arguments)
+                    this.emit('mutate', {
+                        method: method,
+                        args: aproto.slice.call(arguments),
+                        result: result
+                    })
+                }
+            })(method)
+        }
+        for (method in arrayAugmentations) {
             collection[method] = arrayAugmentations[method]
         }
     }