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

feat($resource): pass resource to the dynamic param function #4899

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ngResource/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,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); }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add public docs for this change?

otherwise it looks good.

ids[key] = value && value.charAt && value.charAt(0) == '@' ?
lookupDottedPath(data, value.substr(1)) : value;
});
Expand Down
24 changes: 23 additions & 1 deletion test/ngResource/resourceSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,6 @@ describe("resource", function() {
var currentGroup = 'students',
Person = $resource('/Person/:group/:id', { group: function() { return currentGroup; }});


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

var fedor = Person.get({id: 'fedor'});
Expand All @@ -509,6 +508,29 @@ describe("resource", 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: '[email protected]', count: 1});

var fedor = Person.get();
$httpBackend.flush();

expect(fedor).toEqualData({id: 'fedor', email: '[email protected]', count: 1});

$httpBackend.expect('POST', '/Person/fedor').respond(
{id: 'fedor', email: '[email protected]', count: 2});
fedor.$save();
$httpBackend.flush();
expect(fedor).toEqualData({id: 'fedor', email: '[email protected]', count: 2});
});


it('should support dynamic default parameters (action specific)', function() {
var currentGroup = 'students',
Person = $resource('/Person/:group/:id', {}, {
Expand Down