mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-05-17 11:11:05 +00:00
feat(ngResource): support all $http.config actions
This allows the transformation of the $http request in both directions, headers, caching, and timeout.
This commit is contained in:
parent
224d7d6e90
commit
af89daf464
2 changed files with 60 additions and 17 deletions
|
|
@ -37,24 +37,40 @@
|
||||||
* the data object (useful for non-GET operations).
|
* the data object (useful for non-GET operations).
|
||||||
*
|
*
|
||||||
* @param {Object.<Object>=} actions Hash with declaration of custom action that should extend the
|
* @param {Object.<Object>=} actions Hash with declaration of custom action that should extend the
|
||||||
* default set of resource actions. The declaration should be created in the following format:
|
* default set of resource actions. The declaration should be created in the format of {@link
|
||||||
|
* ng.$http#Parameters $http.config}:
|
||||||
*
|
*
|
||||||
* {action1: {method:?, params:?, isArray:?, headers:?},
|
* {action1: {method:?, params:?, isArray:?, headers:?, ...},
|
||||||
* action2: {method:?, params:?, isArray:?, headers:?},
|
* action2: {method:?, params:?, isArray:?, headers:?, ...},
|
||||||
* ...}
|
* ...}
|
||||||
*
|
*
|
||||||
* Where:
|
* Where:
|
||||||
*
|
*
|
||||||
* - `action` – {string} – The name of action. This name becomes the name of the method on your
|
* - **`action`** – {string} – The name of action. This name becomes the name of the method on your
|
||||||
* resource object.
|
* resource object.
|
||||||
* - `method` – {string} – HTTP request method. Valid methods are: `GET`, `POST`, `PUT`, `DELETE`,
|
* - **`method`** – {string} – HTTP request method. Valid methods are: `GET`, `POST`, `PUT`, `DELETE`,
|
||||||
* and `JSONP`
|
* and `JSONP`.
|
||||||
* - `params` – {Object=} – Optional set of pre-bound parameters for this action. If any of the
|
* - **`params`** – {Object=} – Optional set of pre-bound parameters for this action. If any of the
|
||||||
* parameter value is a function, it will be executed every time when a param value needs to be
|
* parameter value is a function, it will be executed every time when a param value needs to be
|
||||||
* obtained for a request (unless the param was overriden).
|
* obtained for a request (unless the param was overriden).
|
||||||
* - isArray – {boolean=} – If true then the returned object for this action is an array, see
|
* - **`isArray`** – {boolean=} – If true then the returned object for this action is an array, see
|
||||||
* `returns` section.
|
* `returns` section.
|
||||||
* - `headers` – {Object=} – Optional HTTP headers to send
|
* - **`transformRequest`** – `{function(data, headersGetter)|Array.<function(data, headersGetter)>}` –
|
||||||
|
* transform function or an array of such functions. The transform function takes the http
|
||||||
|
* request body and headers and returns its transformed (typically serialized) version.
|
||||||
|
* - **`transformResponse`** – `{function(data, headersGetter)|Array.<function(data, headersGetter)>}` –
|
||||||
|
* transform function or an array of such functions. The transform function takes the http
|
||||||
|
* response body and headers and returns its transformed (typically deserialized) version.
|
||||||
|
* - **`cache`** – `{boolean|Cache}` – If true, a default $http cache will be used to cache the
|
||||||
|
* GET request, otherwise if a cache instance built with
|
||||||
|
* {@link ng.$cacheFactory $cacheFactory}, this cache will be used for
|
||||||
|
* caching.
|
||||||
|
* - **`timeout`** – `{number}` – timeout in milliseconds.
|
||||||
|
* - **`withCredentials`** - `{boolean}` - whether to to set the `withCredentials` flag on the
|
||||||
|
* XHR object. See {@link https://developer.mozilla.org/en/http_access_control#section_5
|
||||||
|
* requests with credentials} for more information.
|
||||||
|
* - **`responseType`** - `{string}` - see {@link
|
||||||
|
* https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest#responseType requestType}.
|
||||||
*
|
*
|
||||||
* @returns {Object} A resource "class" object with methods for the default set of resource actions
|
* @returns {Object} A resource "class" object with methods for the default set of resource actions
|
||||||
* optionally extended with custom `actions`. The default set contains these actions:
|
* optionally extended with custom `actions`. The default set contains these actions:
|
||||||
|
|
@ -374,12 +390,17 @@ 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));
|
||||||
$http({
|
var httpConfig = {};
|
||||||
method: action.method,
|
|
||||||
url: route.url(extend({}, extractParams(data, action.params || {}), params)),
|
forEach(action, function(value, key) {
|
||||||
data: data,
|
if (key != 'params' && key != 'isArray' ) {
|
||||||
headers: extend({}, action.headers || {})
|
httpConfig[key] = copy(value);
|
||||||
}).then(function(response) {
|
}
|
||||||
|
});
|
||||||
|
httpConfig.data = data;
|
||||||
|
httpConfig.url = route.url(extend({}, extractParams(data, action.params || {}), params))
|
||||||
|
|
||||||
|
$http(httpConfig).then(function(response) {
|
||||||
var data = response.data;
|
var data = response.data;
|
||||||
|
|
||||||
if (data) {
|
if (data) {
|
||||||
|
|
|
||||||
|
|
@ -427,4 +427,26 @@ describe("resource", function() {
|
||||||
expect(callback).not.toHaveBeenCalled();
|
expect(callback).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
it('should transform request/response', function() {
|
||||||
|
var Person = $resource('/Person/:id', {}, {
|
||||||
|
save: {
|
||||||
|
method: 'POST',
|
||||||
|
params: {id: '@id'},
|
||||||
|
transformRequest: function(data) {
|
||||||
|
return angular.toJson({ __id: data.id });
|
||||||
|
},
|
||||||
|
transformResponse: function(data) {
|
||||||
|
return { id: data.__id };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
$httpBackend.expect('POST', '/Person/123', { __id: 123 }).respond({ __id: 456 });
|
||||||
|
var person = new Person({id:123});
|
||||||
|
person.$save();
|
||||||
|
$httpBackend.flush();
|
||||||
|
expect(person.id).toEqual(456);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue