Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit aa8d783

Browse files
chirayukpetebacondarwin
authored andcommitted
feat($resource): pass the resource to a dynamic param functions
Closes #4899
1 parent 22ec93b commit aa8d783

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

src/ngResource/resource.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,9 @@ function shallowClearAndCopy(src, dst) {
114114
* can escape it with `/\.`.
115115
*
116116
* @param {Object=} paramDefaults Default values for `url` parameters. These can be overridden in
117-
* `actions` methods. If a parameter value is a function, it will be executed every time
118-
* when a param value needs to be obtained for a request (unless the param was overridden).
117+
* `actions` methods. If a parameter value is a function, it will be called every time
118+
* a param value needs to be obtained for a request (unless the param was overridden). The function
119+
* will be passed the current data value as a argument.
119120
*
120121
* Each key value in the parameter object is first bound to url template if present and then any
121122
* excess keys are appended to the url search query after the `?`.
@@ -146,8 +147,9 @@ function shallowClearAndCopy(src, dst) {
146147
* - **`method`** – {string} – Case insensitive HTTP method (e.g. `GET`, `POST`, `PUT`,
147148
* `DELETE`, `JSONP`, etc).
148149
* - **`params`** – {Object=} – Optional set of pre-bound parameters for this action. If any of
149-
* the parameter value is a function, it will be executed every time when a param value needs to
150-
* be obtained for a request (unless the param was overridden).
150+
* the parameter value is a function, it will be called every time when a param value needs to
151+
* be obtained for a request (unless the param was overridden). The function will be passed the
152+
* current data value as a argument.
151153
* - **`url`** – {string} – action specific `url` override. The url templating is supported just
152154
* like for the resource-level urls.
153155
* - **`isArray`** – {boolean=} – If true then the returned object for this action is an array,
@@ -644,7 +646,7 @@ angular.module('ngResource', ['ng']).
644646
var ids = {};
645647
actionParams = extend({}, paramDefaults, actionParams);
646648
forEach(actionParams, function(value, key) {
647-
if (isFunction(value)) { value = value(); }
649+
if (isFunction(value)) { value = value(data); }
648650
ids[key] = value && value.charAt && value.charAt(0) === '@' ?
649651
lookupDottedPath(data, value.substr(1)) : value;
650652
});

test/ngResource/resourceSpec.js

+23-1
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,6 @@ describe("basic usage", function() {
643643
var currentGroup = 'students',
644644
Person = $resource('/Person/:group/:id', { group: function() { return currentGroup; }});
645645

646-
647646
$httpBackend.expect('GET', '/Person/students/fedor').respond({id: 'fedor', email: '[email protected]'});
648647

649648
var fedor = Person.get({id: 'fedor'});
@@ -653,6 +652,29 @@ describe("basic usage", function() {
653652
});
654653

655654

655+
it('should pass resource object to dynamic default parameters', function() {
656+
var Person = $resource('/Person/:id', {
657+
id: function(data) {
658+
return data ? data.id : 'fedor';
659+
}
660+
});
661+
662+
$httpBackend.expect('GET', '/Person/fedor').respond(
663+
{id: 'fedor', email: '[email protected]', count: 1});
664+
665+
var fedor = Person.get();
666+
$httpBackend.flush();
667+
668+
expect(fedor).toEqualData({id: 'fedor', email: '[email protected]', count: 1});
669+
670+
$httpBackend.expect('POST', '/Person/fedor').respond(
671+
{id: 'fedor', email: '[email protected]', count: 2});
672+
fedor.$save();
673+
$httpBackend.flush();
674+
expect(fedor).toEqualData({id: 'fedor', email: '[email protected]', count: 2});
675+
});
676+
677+
656678
it('should support dynamic default parameters (action specific)', function() {
657679
var currentGroup = 'students',
658680
Person = $resource('/Person/:group/:id', {}, {

0 commit comments

Comments
 (0)