Просмотр исходного кода

simplify getter to reduce get events triggered

Evan You 11 лет назад
Родитель
Сommit
ff7228b73c
3 измененных файлов с 8 добавлено и 42 удалено
  1. 4 1
      src/parse/expression.js
  2. 4 29
      src/parse/path.js
  3. 0 12
      test/unit/specs/parse/path_spec.js

+ 4 - 1
src/parse/expression.js

@@ -171,7 +171,10 @@ function compilePathFns (exp) {
 
 
 function makeGetter (body) {
 function makeGetter (body) {
   try {
   try {
-    return new Function('scope', 'return ' + body + ';')
+    return new Function(
+      'scope',
+      'try{return ' + body + '}catch(e){};'
+    )
   } catch (e) {
   } catch (e) {
     _.warn(
     _.warn(
       'Invalid expression. ' + 
       'Invalid expression. ' + 

+ 4 - 29
src/parse/path.js

@@ -225,17 +225,10 @@ function formatAccessor(key) {
  */
  */
 
 
 exports.compileGetter = function (path) {
 exports.compileGetter = function (path) {
-  var body = 'if (o != null'
-  var pathString = 'o'
-  var key
-  for (var i = 0, l = path.length - 1; i < l; i++) {
-    key = path[i]
-    pathString += formatAccessor(key)
-    body += ' && ' + pathString + ' != null'
-  }
-  key = path[i]
-  pathString += formatAccessor(key)
-  body += ') return ' + pathString
+  var body =
+    'try{return o' +
+    path.map(formatAccessor).join('') +
+    '}catch(e){};'
   return new Function('o', body)
   return new Function('o', body)
 }
 }
 
 
@@ -272,24 +265,6 @@ exports.get = function (obj, path) {
   }
   }
 }
 }
 
 
-/**
- * Get from an object from an Observer-delimitered path.
- * e.g. "a\bb\bc"
- *
- * @param {Object} obj
- * @param {String} path
- */
-
-exports.getFromObserver = function (obj, path) {
-  var hit = pathCache.get(path)
-  if (!hit) {
-    hit = path.split(Observer.pathDelimiter)
-    hit.get = exports.compileGetter(hit)
-    pathCache.put(path, hit)
-  }
-  return hit.get(obj)
-}
-
 /**
 /**
  * Set on an object from a path
  * Set on an object from a path
  *
  *

+ 0 - 12
test/unit/specs/parse/path_spec.js

@@ -84,18 +84,6 @@ describe('Path', function () {
     expect(Path.get(obj, 'a.c')).toBeUndefined()
     expect(Path.get(obj, 'a.c')).toBeUndefined()
   })
   })
 
 
-  it('get from observer delimited path', function () {
-    var delim = Observer.pathDelimiter
-    var path = ['a','b','0'].join(delim)
-    var obj = {
-      a: {
-        b: [123]
-      }
-    }
-    expect(Path.getFromObserver(obj, path)).toBe(123)
-    expect(Path.getFromObserver(obj, ['a','c'].join(delim))).toBeUndefined()
-  })
-
   it('set', function () {
   it('set', function () {
     var path = 'a.b.c'
     var path = 'a.b.c'
     var obj = {
     var obj = {