Browse Source

optimize array watch method hijacking

Evan You 12 years ago
parent
commit
a104afb472
2 changed files with 15 additions and 14 deletions
  1. 0 2
      examples/todomvc/js/app.js
  2. 15 12
      src/utils.js

+ 0 - 2
examples/todomvc/js/app.js

@@ -1,5 +1,3 @@
-Seed.config({ debug: false })
-
 var filters = {
     all: function () { return true },
     active: function (todo) { return !todo.completed },

+ 15 - 12
src/utils.js

@@ -2,7 +2,6 @@ var config        = require('./config'),
     Emitter       = require('emitter'),
     toString      = Object.prototype.toString,
     aproto        = Array.prototype,
-    arrayMutators = ['push','pop','shift','unshift','splice','sort','reverse'],
     templates     = {}
 
 var arrayAugmentations = {
@@ -16,6 +15,20 @@ var arrayAugmentations = {
     }
 }
 
+var arrayMutators = ['push','pop','shift','unshift','splice','sort','reverse'],
+    mutationInterceptors = {}
+
+arrayMutators.forEach(function (method) {
+    mutationInterceptors[method] = function () {
+        var result = aproto[method].apply(this, arguments)
+        this.emit('mutate', {
+            method: method,
+            args: aproto.slice.call(arguments),
+            result: result
+        })
+    }
+})
+
 /*
  *  get accurate type of an object
  */
@@ -87,17 +100,7 @@ module.exports = {
         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)
+            collection[method] = mutationInterceptors[method]
         }
         for (method in arrayAugmentations) {
             collection[method] = arrayAugmentations[method]