diff options
| author | Misko Hevery | 2013-01-17 22:01:50 -0800 | 
|---|---|---|
| committer | Misko Hevery | 2013-01-17 23:08:39 -0800 | 
| commit | af89daf4641f57b92be6c1f3635f5a3237f20c71 (patch) | |
| tree | 8dd01114b497cdc468bdf378ba3c841ad9e5f005 | |
| parent | 224d7d6e9023395e9bf3f7217984c04fd550f4e7 (diff) | |
| download | angular.js-af89daf4641f57b92be6c1f3635f5a3237f20c71.tar.bz2 | |
feat(ngResource): support all $http.config actions
This allows the transformation of the $http request in both directions,
headers, caching, and timeout.
| -rw-r--r-- | src/ngResource/resource.js | 55 | ||||
| -rw-r--r-- | test/ngResource/resourceSpec.js | 22 | 
2 files changed, 60 insertions, 17 deletions
| diff --git a/src/ngResource/resource.js b/src/ngResource/resource.js index dde7f2c0..b2bf86cb 100644 --- a/src/ngResource/resource.js +++ b/src/ngResource/resource.js @@ -37,24 +37,40 @@   *   the data object (useful for non-GET operations).   *   * @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:?}, - *        action2: {method:?, params:?, isArray:?, headers:?}, + *       {action1: {method:?, params:?, isArray:?, headers:?, ...}, + *        action2: {method:?, params:?, isArray:?, headers:?, ...},   *        ...}   *   *   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. - *   - `method` – {string} – HTTP request method. Valid methods are: `GET`, `POST`, `PUT`, `DELETE`, - *     and `JSONP` - *   - `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 -  *    obtained for a request (unless the param was overriden). - *   - isArray – {boolean=} – If true then the returned object for this action is an array, see + *   - **`method`** – {string} – HTTP request method. Valid methods are: `GET`, `POST`, `PUT`, `DELETE`, + *     and `JSONP`. + *   - **`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 + *     obtained for a request (unless the param was overriden). + *   - **`isArray`** – {boolean=} – If true then the returned object for this action is an array, see   *     `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   *   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)); -          $http({ -            method: action.method, -            url: route.url(extend({}, extractParams(data, action.params || {}), params)), -            data: data, -            headers: extend({}, action.headers || {}) -          }).then(function(response) { +          var httpConfig = {}; + +          forEach(action, function(value, key) { +            if (key != 'params' && key != 'isArray' ) { +              httpConfig[key] = copy(value); +            } +          }); +          httpConfig.data = data; +          httpConfig.url = route.url(extend({}, extractParams(data, action.params || {}), params)) + +          $http(httpConfig).then(function(response) {                var data = response.data;                if (data) { diff --git a/test/ngResource/resourceSpec.js b/test/ngResource/resourceSpec.js index b9041426..9373d347 100644 --- a/test/ngResource/resourceSpec.js +++ b/test/ngResource/resourceSpec.js @@ -427,4 +427,26 @@ describe("resource", function() {        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); +  });  }); | 
