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

Commit cc42c99

Browse files
lascapIgorMinar
authored andcommitted
feat($resource): allow dynamic default parameters
Default resource params can now be calculated at runtime if defined via a function.
1 parent 3c7bfa7 commit cc42c99

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

src/ngResource/resource.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
* `/user/:username`.
2323
*
2424
* @param {Object=} paramDefaults Default values for `url` parameters. These can be overridden in
25-
* `actions` methods.
25+
* `actions` methods. If any of the parameter value is a function, it will be executed every time
26+
* when a param value needs to be obtained for a request (unless the param was overriden).
2627
*
2728
* Each key value in the parameter object is first bound to url template if present and then any
2829
* excess keys are appended to the url search query after the `?`.
@@ -46,10 +47,12 @@
4647
* resource object.
4748
* - `method` – {string} – HTTP request method. Valid methods are: `GET`, `POST`, `PUT`, `DELETE`,
4849
* and `JSONP`
49-
* - `params` – {object=} – Optional set of pre-bound parameters for this action.
50+
* - `params` – {Object=} – Optional set of pre-bound parameters for this action. If any of the
51+
* parameter value is a function, it will be executed every time when a param value needs to be
52+
* obtained for a request (unless the param was overriden).
5053
* - isArray – {boolean=} – If true then the returned object for this action is an array, see
5154
* `returns` section.
52-
* - `headers` – {object=} – Optional HTTP headers to send
55+
* - `headers` – {Object=} – Optional HTTP headers to send
5356
*
5457
* @returns {Object} A resource "class" object with methods for the default set of resource actions
5558
* optionally extended with custom `actions`. The default set contains these actions:
@@ -316,6 +319,7 @@ angular.module('ngResource', ['ng']).
316319
var ids = {};
317320
actionParams = extend({}, paramDefaults, actionParams);
318321
forEach(actionParams, function(value, key){
322+
if (isFunction(value)) { value = value(); }
319323
ids[key] = value.charAt && value.charAt(0) == '@' ? getter(data, value.substr(1)) : value;
320324
});
321325
return ids;

test/ngResource/resourceSpec.js

+29
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,35 @@ describe("resource", function() {
356356
});
357357

358358

359+
it('should support dynamic default parameters (global)', function() {
360+
var currentGroup = 'students',
361+
Person = $resource('/Person/:group/:id', { group: function() { return currentGroup; }});
362+
363+
364+
$httpBackend.expect('GET', '/Person/students/fedor').respond({id: 'fedor', email: '[email protected]'});
365+
366+
var fedor = Person.get({id: 'fedor'});
367+
$httpBackend.flush();
368+
369+
expect(fedor).toEqualData({id: 'fedor', email: '[email protected]'});
370+
});
371+
372+
373+
it('should support dynamic default parameters (action specific)', function() {
374+
var currentGroup = 'students',
375+
Person = $resource('/Person/:group/:id', {}, {
376+
fetch: {method: 'GET', params: {group: function() { return currentGroup; }}}
377+
});
378+
379+
$httpBackend.expect('GET', '/Person/students/fedor').respond({id: 'fedor', email: '[email protected]'});
380+
381+
var fedor = Person.fetch({id: 'fedor'});
382+
$httpBackend.flush();
383+
384+
expect(fedor).toEqualData({id: 'fedor', email: '[email protected]'});
385+
});
386+
387+
359388
it('should exercise full stack', function() {
360389
var Person = $resource('/Person/:id');
361390

0 commit comments

Comments
 (0)