aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJussi Kosunen2013-08-15 15:14:54 +0300
committerVojta Jina2013-08-15 15:15:27 -0700
commit3a65822023119b71deab5e298c7ef2de204caa13 (patch)
treea10f4c3b80f786401b62cbd92e7941420c85f687
parent37123cd2858b4e318ed8109af745312df4848577 (diff)
downloadangular.js-3a65822023119b71deab5e298c7ef2de204caa13.tar.bz2
fix($parse): handle promises returned from parsed function calls
When a parsed function call returns a promise, the evaluated value is the resolved value of the promise rather than the promise object. Closes #3503
-rw-r--r--src/ng/parse.js14
-rw-r--r--test/ng/parseSpec.js12
2 files changed, 25 insertions, 1 deletions
diff --git a/src/ng/parse.js b/src/ng/parse.js
index 9eeb1439..5597acd8 100644
--- a/src/ng/parse.js
+++ b/src/ng/parse.js
@@ -689,9 +689,21 @@ function parser(text, json, $filter, csp){
}
var fnPtr = fn(scope, locals, context) || noop;
// IE stupidity!
- return fnPtr.apply
+ var v = fnPtr.apply
? fnPtr.apply(context, args)
: fnPtr(args[0], args[1], args[2], args[3], args[4]);
+
+ // Check for promise
+ if (v && v.then) {
+ var p = v;
+ if (!('$$v' in v)) {
+ p.$$v = undefined;
+ p.then(function(val) { p.$$v = val; });
+ }
+ v = v.$$v;
+ }
+
+ return v;
};
}
diff --git a/test/ng/parseSpec.js b/test/ng/parseSpec.js
index f9a80fb6..7673066b 100644
--- a/test/ng/parseSpec.js
+++ b/test/ng/parseSpec.js
@@ -846,6 +846,18 @@ describe('parser', function() {
expect(scope.$eval('greeting')).toBe(undefined);
});
+ it('should evaluate a function call returning a promise and eventually get its return value', function() {
+ scope.greetingFn = function() { return promise; };
+ expect(scope.$eval('greetingFn()')).toBe(undefined);
+
+ scope.$digest();
+ expect(scope.$eval('greetingFn()')).toBe(undefined);
+
+ deferred.resolve('hello!');
+ expect(scope.$eval('greetingFn()')).toBe(undefined);
+ scope.$digest();
+ expect(scope.$eval('greetingFn()')).toBe('hello!');
+ });
describe('assignment into promises', function() {
// This behavior is analogous to assignments to non-promise values