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

Commit

Permalink
feat($resource): allow dynamic default parameters
Browse files Browse the repository at this point in the history
Default resource params can now be calculated at runtime if defined
via a function.
  • Loading branch information
lascap authored and IgorMinar committed Nov 28, 2012
1 parent 3c7bfa7 commit cc42c99
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/ngResource/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
* `/user/:username`.
*
* @param {Object=} paramDefaults Default values for `url` parameters. These can be overridden in
* `actions` methods.
* `actions` methods. 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).
*
* 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 `?`.
Expand All @@ -46,10 +47,12 @@
* 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.
* - `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
* - `headers` – {Object=} – Optional HTTP headers to send
*
* @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:
Expand Down Expand Up @@ -316,6 +319,7 @@ angular.module('ngResource', ['ng']).
var ids = {};
actionParams = extend({}, paramDefaults, actionParams);
forEach(actionParams, function(value, key){
if (isFunction(value)) { value = value(); }
ids[key] = value.charAt && value.charAt(0) == '@' ? getter(data, value.substr(1)) : value;
});
return ids;
Expand Down
29 changes: 29 additions & 0 deletions test/ngResource/resourceSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,35 @@ describe("resource", function() {
});


it('should support dynamic default parameters (global)', 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'});
$httpBackend.flush();

expect(fedor).toEqualData({id: 'fedor', email: 'f@f.com'});
});


it('should support dynamic default parameters (action specific)', function() {
var currentGroup = 'students',
Person = $resource('/Person/:group/:id', {}, {
fetch: {method: 'GET', params: {group: function() { return currentGroup; }}}
});

$httpBackend.expect('GET', '/Person/students/fedor').respond({id: 'fedor', email: 'f@f.com'});

var fedor = Person.fetch({id: 'fedor'});
$httpBackend.flush();

expect(fedor).toEqualData({id: 'fedor', email: 'f@f.com'});
});


it('should exercise full stack', function() {
var Person = $resource('/Person/:id');

Expand Down

0 comments on commit cc42c99

Please sign in to comment.