aboutsummaryrefslogtreecommitdiffstats
path: root/src/ngResource/resource.js
diff options
context:
space:
mode:
authorPartap Davis2013-01-17 14:21:35 -0800
committerMisko Hevery2013-01-23 20:57:26 -0800
commitf3bff27460afb3be208a05959d5b84233d34b7eb (patch)
treefe129b7ef63a99a11fc44bd3d4e77487e278c419 /src/ngResource/resource.js
parent4df45b20d460239a0f5001fb0dd59f95e2d0e80d (diff)
downloadangular.js-f3bff27460afb3be208a05959d5b84233d34b7eb.tar.bz2
feat(resource): add $q/$resorved property to Resource
Diffstat (limited to 'src/ngResource/resource.js')
-rw-r--r--src/ngResource/resource.js23
1 files changed, 20 insertions, 3 deletions
diff --git a/src/ngResource/resource.js b/src/ngResource/resource.js
index 7e26a6a4..037a12ae 100644
--- a/src/ngResource/resource.js
+++ b/src/ngResource/resource.js
@@ -109,6 +109,11 @@
* - non-GET "class" actions: `Resource.action([parameters], postData, [success], [error])`
* - non-GET instance actions: `instance.$action([parameters], [success], [error])`
*
+ * The Resource also has these properties:
+ *
+ * - '$q': the promise from the underlying {@link ng.$http} call.
+ * - '$resolved': true if the promise has been resolved (either with success or rejection);
+ * Knowing if the Resource has been resolved is useful in data-binding.
*
* @example
*
@@ -362,6 +367,8 @@ angular.module('ngResource', ['ng']).
var data;
var success = noop;
var error = null;
+ var promise;
+
switch(arguments.length) {
case 4:
error = a4;
@@ -397,7 +404,8 @@ angular.module('ngResource', ['ng']).
}
var value = this instanceof Resource ? this : (action.isArray ? [] : new Resource(data));
- var httpConfig = {};
+ var httpConfig = {},
+ promise;
forEach(action, function(value, key) {
if (key != 'params' && key != 'isArray' ) {
@@ -407,9 +415,15 @@ angular.module('ngResource', ['ng']).
httpConfig.data = data;
httpConfig.url = route.url(extend({}, extractParams(data, action.params || {}), params))
- $http(httpConfig).then(function(response) {
- var data = response.data;
+ function markResolved() { value.$resolved = true; };
+ promise = $http(httpConfig);
+ value.$q = promise;
+ value.$resolved = false;
+ promise.then(markResolved, markResolved)
+ promise.then(function(response) {
+ var data = response.data;
+ var q = value.$q, resolved = value.$resolved;
if (data) {
if (action.isArray) {
value.length = 0;
@@ -418,12 +432,15 @@ angular.module('ngResource', ['ng']).
});
} else {
copy(data, value);
+ value.$q = q;
+ value.$resolved = resolved;
}
}
(success||noop)(value, response.headers);
}, error);
return value;
+
};