feat($resource): pass the resource to a dynamic param functions

Closes #4899
This commit is contained in:
Chirayu Krishnappa
2013-11-11 16:34:22 -08:00
committed by Peter Bacon Darwin
parent 22ec93be8f
commit aa8d783cff
2 changed files with 30 additions and 6 deletions
+7 -5
View File
@@ -114,8 +114,9 @@ function shallowClearAndCopy(src, dst) {
* can escape it with `/\.`.
*
* @param {Object=} paramDefaults Default values for `url` parameters. These can be overridden in
* `actions` methods. If a 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 overridden).
* `actions` methods. If a parameter value is a function, it will be called every time
* a param value needs to be obtained for a request (unless the param was overridden). The function
* will be passed the current data value as a argument.
*
* Each key value in the parameter object is first bound to url template if present and then any
* excess keys are appended to the url search query after the `?`.
@@ -146,8 +147,9 @@ function shallowClearAndCopy(src, dst) {
* - **`method`** {string} Case insensitive HTTP method (e.g. `GET`, `POST`, `PUT`,
* `DELETE`, `JSONP`, etc).
* - **`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 overridden).
* the parameter value is a function, it will be called every time when a param value needs to
* be obtained for a request (unless the param was overridden). The function will be passed the
* current data value as a argument.
* - **`url`** {string} action specific `url` override. The url templating is supported just
* like for the resource-level urls.
* - **`isArray`** {boolean=} If true then the returned object for this action is an array,
@@ -644,7 +646,7 @@ angular.module('ngResource', ['ng']).
var ids = {};
actionParams = extend({}, paramDefaults, actionParams);
forEach(actionParams, function(value, key) {
if (isFunction(value)) { value = value(); }
if (isFunction(value)) { value = value(data); }
ids[key] = value && value.charAt && value.charAt(0) === '@' ?
lookupDottedPath(data, value.substr(1)) : value;
});
+23 -1
View File
@@ -643,7 +643,6 @@ describe("basic usage", function() {
var currentGroup = 'students',
Person = $resource('/Person/:group/:id', { group: function() { return currentGroup; }});
$httpBackend.expect('GET', '/Person/students/fedor').respond({id: 'fedor', email: 'f@f.com'});
var fedor = Person.get({id: 'fedor'});
@@ -653,6 +652,29 @@ describe("basic usage", function() {
});
it('should pass resource object to dynamic default parameters', function() {
var Person = $resource('/Person/:id', {
id: function(data) {
return data ? data.id : 'fedor';
}
});
$httpBackend.expect('GET', '/Person/fedor').respond(
{id: 'fedor', email: 'f@f.com', count: 1});
var fedor = Person.get();
$httpBackend.flush();
expect(fedor).toEqualData({id: 'fedor', email: 'f@f.com', count: 1});
$httpBackend.expect('POST', '/Person/fedor').respond(
{id: 'fedor', email: 'f@f.com', count: 2});
fedor.$save();
$httpBackend.flush();
expect(fedor).toEqualData({id: 'fedor', email: 'f@f.com', count: 2});
});
it('should support dynamic default parameters (action specific)', function() {
var currentGroup = 'students',
Person = $resource('/Person/:group/:id', {}, {