Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
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
13 changes: 9 additions & 4 deletions src/ngResource/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@
// Define CreditCard class
var CreditCard = $resource('/user/:userId/card/:cardId',
{userId:123, cardId:'@id'}, {
charge: {method:'POST', params:{charge:true}}
charge: {method:'POST', params:{charge:true}},
block: {method:'POST', url:'/user/:userId/card/:cardId/block'}
});

// We can retrieve a collection from the server
Expand All @@ -143,6 +144,10 @@
// our custom method is mapped as well.
card.$charge({amount:9.99});
// POST: /user/123/card/456?amount=9.99&charge=true {id:456, number:'1234', name:'J. Smith'}

// you can also override url in custom methods.
card.$block();
// POST: /user/123/card/456/block
});

// we can create an instance as well
Expand Down Expand Up @@ -303,13 +308,13 @@ angular.module('ngResource', ['ng']).
}

Route.prototype = {
url: function(params) {
url: function(params, url) {
var self = this,
url = this.template,
val,
encodedVal;

params = params || {};
url = url || this.template;
forEach(this.urlParams, function(_, urlParam){
val = params.hasOwnProperty(urlParam) ? params[urlParam] : self.defaults[urlParam];
if (angular.isDefined(val) && val !== null) {
Expand Down Expand Up @@ -413,7 +418,7 @@ angular.module('ngResource', ['ng']).
}
});
httpConfig.data = data;
httpConfig.url = route.url(extend({}, extractParams(data, action.params || {}), params))
httpConfig.url = route.url(extend({}, extractParams(data, action.params || {}), params), action.url)

function markResolved() { value.$resolved = true; };

Expand Down
13 changes: 13 additions & 0 deletions test/ngResource/resourceSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -521,4 +521,17 @@ describe("resource", function() {
$httpBackend.flush();
expect(person.id).toEqual(456);
});

it('should support override url template (action specific)', function() {
var Person = $resource('/Person/:id', {}, {
best: {method: 'GET', url: '/Person/best'}
});

$httpBackend.expect('GET', '/Person/best').respond({id: 'guillaume', email: 'g@g.com'});

var bestPerson = Person.best();
$httpBackend.flush();

expect(bestPerson).toEqualData({id: 'guillaume', email: 'g@g.com'});
});
});