From 78b6e8a446c0e38075c14b724f3cdf345c01fa06 Mon Sep 17 00:00:00 2001 From: Igor Minar Date: Wed, 23 Nov 2011 15:53:06 -0800 Subject: feat($parse): add support for transparent evaluation of Promises Parser now builds expressions that can detect promises and transparently evaluate them to undefined or the promise value. If promiseA is resolved with value 'A', then {{promiseA}} evals to 'A'; If promiseA is unresolved, then {{promiseA}} evals to undefined; Following invocations are supported: - {{promise}} - {{promise.futureProp}} - {{[promise][0]}} - {{object.promise}} - {{object[promise]}} - {{array[promise]}} - {{fn(promise)}} - combinations of the above --- src/service/parse.js | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/service/parse.js b/src/service/parse.js index 432929b5..3a6c4106 100644 --- a/src/service/parse.js +++ b/src/service/parse.js @@ -522,9 +522,21 @@ function parser(text, json, $filter){ consume(']'); return extend( function(self){ - var o = obj(self); - var i = indexFn(self); - return (o) ? o[i] : undefined; + var o = obj(self), + i = indexFn(self), + v, p; + + if (!o) return undefined; + v = o[i]; + if (v && v.then) { + p = v; + if (!('$$v' in v)) { + p.$$v = undefined; + p.then(function(val) { p.$$v = val; }); + } + v = v.$$v; + } + return v; }, { assign:function(self, value){ return obj(self)[indexFn(self)] = value; @@ -673,7 +685,7 @@ function getterFn(path) { var fn = getterFnCache[path]; if (fn) return fn; - var code = 'var l, fn, t;\n'; + var code = 'var l, fn, p;\n'; forEach(path.split('.'), function(key) { key = (JS_KEYWORDS[key]) ? '["' + key + '"]' : '.' + key; code += 'if(!s) return s;\n' + @@ -683,11 +695,18 @@ function getterFn(path) { ' fn=function(){ return l' + key + '.apply(l, arguments); };\n' + ' fn.$unboundFn=s;\n' + ' s=fn;\n' + + '} else if (s && s.then) {\n' + + ' if (!("$$v" in s)) {\n' + + ' p=s;\n' + + ' p.$$v = undefined;\n' + + ' p.then(function(v) {p.$$v=v;});\n' + + '}\n' + + ' s=s.$$v\n' + '}\n'; }); code += 'return s;'; fn = Function('s', code); - fn["toString"] = function() { return code; }; + fn.toString = function() { return code; }; return getterFnCache[path] = fn; } -- cgit v1.2.3