mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-05-26 06:43:43 +00:00
feat(resource): add $q/$resorved property to Resource
This commit is contained in:
parent
4df45b20d4
commit
f3bff27460
2 changed files with 68 additions and 4 deletions
|
|
@ -109,6 +109,11 @@
|
||||||
* - non-GET "class" actions: `Resource.action([parameters], postData, [success], [error])`
|
* - non-GET "class" actions: `Resource.action([parameters], postData, [success], [error])`
|
||||||
* - non-GET instance actions: `instance.$action([parameters], [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
|
* @example
|
||||||
*
|
*
|
||||||
|
|
@ -362,6 +367,8 @@ angular.module('ngResource', ['ng']).
|
||||||
var data;
|
var data;
|
||||||
var success = noop;
|
var success = noop;
|
||||||
var error = null;
|
var error = null;
|
||||||
|
var promise;
|
||||||
|
|
||||||
switch(arguments.length) {
|
switch(arguments.length) {
|
||||||
case 4:
|
case 4:
|
||||||
error = a4;
|
error = a4;
|
||||||
|
|
@ -397,7 +404,8 @@ angular.module('ngResource', ['ng']).
|
||||||
}
|
}
|
||||||
|
|
||||||
var value = this instanceof Resource ? this : (action.isArray ? [] : new Resource(data));
|
var value = this instanceof Resource ? this : (action.isArray ? [] : new Resource(data));
|
||||||
var httpConfig = {};
|
var httpConfig = {},
|
||||||
|
promise;
|
||||||
|
|
||||||
forEach(action, function(value, key) {
|
forEach(action, function(value, key) {
|
||||||
if (key != 'params' && key != 'isArray' ) {
|
if (key != 'params' && key != 'isArray' ) {
|
||||||
|
|
@ -407,9 +415,15 @@ angular.module('ngResource', ['ng']).
|
||||||
httpConfig.data = data;
|
httpConfig.data = data;
|
||||||
httpConfig.url = route.url(extend({}, extractParams(data, action.params || {}), params))
|
httpConfig.url = route.url(extend({}, extractParams(data, action.params || {}), params))
|
||||||
|
|
||||||
$http(httpConfig).then(function(response) {
|
function markResolved() { value.$resolved = true; };
|
||||||
var data = response.data;
|
|
||||||
|
|
||||||
|
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 (data) {
|
||||||
if (action.isArray) {
|
if (action.isArray) {
|
||||||
value.length = 0;
|
value.length = 0;
|
||||||
|
|
@ -418,12 +432,15 @@ angular.module('ngResource', ['ng']).
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
copy(data, value);
|
copy(data, value);
|
||||||
|
value.$q = q;
|
||||||
|
value.$resolved = resolved;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
(success||noop)(value, response.headers);
|
(success||noop)(value, response.headers);
|
||||||
}, error);
|
}, error);
|
||||||
|
|
||||||
return value;
|
return value;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -264,7 +264,7 @@ describe("resource", function() {
|
||||||
$httpBackend.expect('GET', '/CreditCard?key=value').respond([{id: 1}, {id: 2}]);
|
$httpBackend.expect('GET', '/CreditCard?key=value').respond([{id: 1}, {id: 2}]);
|
||||||
|
|
||||||
var ccs = CreditCard.query({key: 'value'}, callback);
|
var ccs = CreditCard.query({key: 'value'}, callback);
|
||||||
expect(ccs).toEqual([]);
|
expect(ccs).toEqualData([]);
|
||||||
expect(callback).not.toHaveBeenCalled();
|
expect(callback).not.toHaveBeenCalled();
|
||||||
|
|
||||||
$httpBackend.flush();
|
$httpBackend.flush();
|
||||||
|
|
@ -419,6 +419,53 @@ describe("resource", function() {
|
||||||
expect(person.name).toEqual('misko');
|
expect(person.name).toEqual('misko');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should have $q and $resolved properties for get", function () {
|
||||||
|
$httpBackend.expect('GET', '/CreditCard/123').respond({id: 123, number: '9876'});
|
||||||
|
var cc = CreditCard.get({id: 123}, callback);
|
||||||
|
expect(cc.$q).toBeDefined();
|
||||||
|
expect(typeof cc.$q).toBe('object');
|
||||||
|
expect(cc.$resolved).toBeFalsy();
|
||||||
|
$httpBackend.flush();
|
||||||
|
expect(cc.$q).toBeDefined();
|
||||||
|
expect(cc.$resolved).toBeTruthy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should have $q and $resolved properties for query", function() {
|
||||||
|
$httpBackend.expect('GET', '/CreditCard?key=value').respond([{id: 1}, {id: 2}]);
|
||||||
|
|
||||||
|
var ccs = CreditCard.query({key: 'value'}, callback);
|
||||||
|
expect(ccs.$q).toBeDefined();
|
||||||
|
expect(typeof ccs.$q).toBe('object');
|
||||||
|
expect(ccs.$resolved).toBeFalsy();
|
||||||
|
$httpBackend.flush();
|
||||||
|
expect(ccs.$q).toBeDefined();
|
||||||
|
expect(ccs.$resolved).toBeTruthy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should have $q and $resolved properties for save", function() {
|
||||||
|
$httpBackend.expect('POST', '/CreditCard/123', '{"id":{"key":123},"name":"misko"}').
|
||||||
|
respond({id: {key: 123}, name: 'rama'});
|
||||||
|
|
||||||
|
var cc = CreditCard.save({id: {key: 123}, name: 'misko'}, callback);
|
||||||
|
expect(cc.$q).toBeDefined();
|
||||||
|
expect(typeof cc.$q).toBe('object');
|
||||||
|
expect(cc.$resolved).toBeFalsy();
|
||||||
|
$httpBackend.flush();
|
||||||
|
expect(cc.$q).toBeDefined();
|
||||||
|
expect(cc.$resolved).toBeTruthy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should should have $q and $resolved properties for delete', function() {
|
||||||
|
$httpBackend.expect('DELETE', '/CreditCard/123').respond({});
|
||||||
|
var removed = CreditCard.remove({id:123}, callback);
|
||||||
|
expect(removed.$q).toBeDefined();
|
||||||
|
expect(typeof removed.$q).toBe('object');
|
||||||
|
expect(removed.$resolved).toBeFalsy();
|
||||||
|
$httpBackend.flush();
|
||||||
|
expect(removed.$q).toBeDefined();
|
||||||
|
expect(removed.$resolved).toBeTruthy();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
describe('failure mode', function() {
|
describe('failure mode', function() {
|
||||||
var ERROR_CODE = 500,
|
var ERROR_CODE = 500,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue