瀏覽代碼

use simple bind instead of native bind, remove vm.$get()

Evan You 12 年之前
父節點
當前提交
6204f8bf0f
共有 6 個文件被更改,包括 18 次插入32 次删除
  1. 5 3
      src/compiler.js
  2. 1 4
      src/directive.js
  3. 10 0
      src/utils.js
  4. 0 17
      src/viewmodel.js
  5. 2 2
      test/unit/specs/directive.js
  6. 0 6
      test/unit/specs/viewmodel.js

+ 5 - 3
src/compiler.js

@@ -507,10 +507,12 @@ CompilerProto.markComputed = function (binding) {
     binding.isComputed = true
     // bind the accessors to the vm
     if (binding.isFn) {
-        binding.value = value.bind(vm)
+        binding.value = utils.bind(value, vm)
     } else {
-        value.$get = value.$get.bind(vm)
-        if (value.$set) value.$set = value.$set.bind(vm)
+        value.$get = utils.bind(value.$get, vm)
+        if (value.$set) {
+            value.$set = utils.bind(value.$set, vm)
+        }
     }
     // keep track for dep parsing later
     this.computed.push(binding)

+ 1 - 4
src/directive.js

@@ -148,10 +148,7 @@ DirProto.refresh = function (value) {
     if (this.isFn) {
         value = this.value
     } else {
-        value = this.value.$get({
-            el: this.el,
-            vm: this.vm
-        })
+        value = this.value.$get()
         if (value !== undefined && value === this.computedValue) return
         this.computedValue = value
     }

+ 10 - 0
src/utils.js

@@ -55,6 +55,16 @@ var utils = module.exports = {
         return toString.call(obj).slice(8, -1)
     },
 
+    /**
+     *  Most simple bind with no arguments
+     *  enough for the usecase and fast than native bind()
+     */
+    bind: function (fn, ctx) {
+        return function () {
+            return fn.call(ctx)
+        }
+    },
+
     /**
      *  Make sure only strings and numbers are output to html
      *  output empty string is value is not string or number

+ 0 - 17
src/viewmodel.js

@@ -29,23 +29,6 @@ def(VMProto, '$set', function (key, value) {
     obj[path[d]] = value
 })
 
-/**
- *  The function for getting a key
- *  which will go up along the prototype chain of the bindings
- *  Used in exp-parser.
- */
-def(VMProto, '$get', function (key) {
-    var path = key.split('.'),
-        obj = getTargetVM(this, path),
-        vm = obj
-    if (!obj) return
-    for (var d = 0, l = path.length; d < l; d++) {
-        obj = obj[path[d]]
-    }
-    if (typeof obj === 'function') obj = obj.bind(vm)
-    return obj
-})
-
 /**
  *  watch a key on the viewmodel for changes
  *  fire callback with new value

+ 2 - 2
test/unit/specs/directive.js

@@ -285,8 +285,8 @@ describe('UNIT: Directive', function () {
             applied = false,
             el = 1, vm = 2,
             value = {
-                $get: function (ctx) {
-                    return ctx.el + ctx.vm
+                $get: function () {
+                    return el + vm
                 }
             }
         d.el = el

+ 0 - 6
test/unit/specs/viewmodel.js

@@ -14,12 +14,6 @@ describe('UNIT: ViewModel', function () {
                 b: arr
             }
         })
-    
-    describe('.$get()', function () {
-        it('should retrieve correct value', function () {
-            assert.strictEqual(vm.$get('a.b.c'), data.b.c)
-        })
-    })
 
     describe('.$set()', function () {
         vm.$set('a.b.c', 54321)