diff options
Diffstat (limited to 'src/Scope.js')
| -rw-r--r-- | src/Scope.js | 49 |
1 files changed, 30 insertions, 19 deletions
diff --git a/src/Scope.js b/src/Scope.js index 35da77fe..27fafc3a 100644 --- a/src/Scope.js +++ b/src/Scope.js @@ -44,14 +44,16 @@ function setter(instance, path, value){ } /////////////////////////////////// - -var getterFnCache = {}; -var JS_KEYWORDS = {}; +var scopeId = 0; + getterFnCache = {}, + compileCache = {}, + JS_KEYWORDS = {}; foreach( - ["break", "const", "continue", "class", "delete", - "do", "while", "for", "function", "if", - "instanceof", "new", "return", "switch", - "this", "throw", "try", "catch", "with"], + ["abstract", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue", "debugger", "default", + "delete", "do", "double", "else", "enum", "export", "extends", "false", "final", "finally", "float", "for", "function", "goto", + "if", "implements", "import", "ininstanceof", "intinterface", "long", "native", "new", "null", "package", "private", + "protected", "public", "return", "short", "static", "super", "switch", "synchronized", "this", "throw", "throws", + "transient", "true", "try", "typeof", "var", "volatile", "void", "while", "with"], function(key){ JS_KEYWORDS[key] = true;} ); function getterFn(path){ @@ -74,7 +76,7 @@ function getterFn(path){ code += ' type = angular.Global.typeOf(last);\n'; code += ' fn = (angular[type.charAt(0).toUpperCase() + type.substring(1)]||{})["' + name + '"];\n'; code += ' if (fn)\n'; - code += ' self = function(){ return fn.apply(last, [last].concat(slice.call(arguments, 0, arguments.length))); };\n'; + code += ' self = function(){ return fn.apply(last, [last].concat(Array.prototype.slice.call(arguments, 0, arguments.length))); };\n'; code += ' }\n'; } }); @@ -87,9 +89,8 @@ function getterFn(path){ /////////////////////////////////// -var compileCache = {}; function expressionCompile(exp){ - if (isFunction(exp)) return exp; + if (typeof exp === 'function') return exp; var fn = compileCache[exp]; if (!fn) { var parser = new Parser(exp); @@ -107,7 +108,6 @@ function errorHandlerFor(element, error) { elementError(element, NG_EXCEPTION, isDefined(error) ? toJson(error) : error); } -var scopeId = 0; function createScope(parent, services, existing) { function Parent(){} function API(){} @@ -129,24 +129,32 @@ function createScope(parent, services, existing) { $set: bind(instance, setter, instance), $eval: function $eval(exp) { - if (exp !== undefined) { - return expressionCompile(exp).apply(instance, slice.call(arguments, 1, arguments.length)); - } else { + var type = typeof exp; + if (type == 'undefined') { for ( var i = 0, iSize = evalLists.sorted.length; i < iSize; i++) { for ( var queue = evalLists.sorted[i], - jSize = queue.length, - j= 0; j < jSize; j++) { + jSize = queue.length, + j= 0; j < jSize; j++) { instance.$tryEval(queue[j].fn, queue[j].handler); } } + } else if (type === 'function') { + return exp.call(instance); + } else if (type === 'string') { + return expressionCompile(exp).call(instance); } }, $tryEval: function (expression, exceptionHandler) { + var type = typeof expression; try { - return expressionCompile(expression).apply(instance, slice.call(arguments, 2, arguments.length)); + if (type == 'function') { + return expression.call(instance); + } else if (type == 'string'){ + return expressionCompile(expression).call(instance); + } } catch (e) { - error(e); + (instance.$log || {error:error}).error(e); if (isFunction(exceptionHandler)) { exceptionHandler(e); } else if (exceptionHandler) { @@ -160,12 +168,15 @@ function createScope(parent, services, existing) { $watch: function(watchExp, listener, exceptionHandler) { var watch = expressionCompile(watchExp), last; + listener = expressionCompile(listener); function watcher(){ var value = watch.call(instance), lastValue = last; if (last !== value) { last = value; - instance.$tryEval(listener, exceptionHandler, value, lastValue); + instance.$tryEval(function(){ + return listener.call(instance, value, lastValue); + }, exceptionHandler); } } instance.$onEval(PRIORITY_WATCH, watcher); |
