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

Commit 10e1c75

Browse files
petrovalexmhevery
authored andcommitted
fix($resource): ignore undefined parameters
- $resource should handle multiple params with same name - ignore slashes of undefined parameters - fix default parameters issue, mentioned in #875 Closes #875 Closes #782
1 parent 6c67719 commit 10e1c75

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

src/ngResource/resource.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -280,12 +280,17 @@ angular.module('ngResource', ['ng']).
280280
url: function(params) {
281281
var self = this,
282282
url = this.template,
283+
val,
283284
encodedVal;
284285

285286
params = params || {};
286287
forEach(this.urlParams, function(_, urlParam){
287-
encodedVal = encodeUriSegment(params[urlParam] || self.defaults[urlParam] || "");
288-
url = url.replace(new RegExp(":" + urlParam + "(\\W)"), encodedVal + "$1");
288+
if (val = (params[urlParam] || self.defaults[urlParam])) {
289+
encodedVal = encodeUriSegment(val);
290+
url = url.replace(new RegExp(":" + urlParam + "(\\W)", "g"), encodedVal + "$1");
291+
} else {
292+
url = url.replace(new RegExp("/?:" + urlParam + "(\\W)", "g"), '$1');
293+
}
289294
});
290295
url = url.replace(/\/?#$/, '');
291296
var query = [];
@@ -306,8 +311,9 @@ angular.module('ngResource', ['ng']).
306311

307312
actions = extend({}, DEFAULT_ACTIONS, actions);
308313

309-
function extractParams(data){
314+
function extractParams(data, actionParams){
310315
var ids = {};
316+
paramDefaults = extend(paramDefaults, actionParams);
311317
forEach(paramDefaults || {}, function(value, key){
312318
ids[key] = value.charAt && value.charAt(0) == '@' ? getter(data, value.substr(1)) : value;
313319
});
@@ -362,7 +368,7 @@ angular.module('ngResource', ['ng']).
362368
var value = this instanceof Resource ? this : (action.isArray ? [] : new Resource(data));
363369
$http({
364370
method: action.method,
365-
url: route.url(extend({}, extractParams(data), action.params || {}, params)),
371+
url: route.url(extend({}, extractParams(data, action.params || {}), params)),
366372
data: data,
367373
headers: extend({}, action.headers || {})
368374
}).then(function(response) {

test/ngResource/resourceSpec.js

+25-2
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,14 @@ describe("resource", function() {
5555
$httpBackend.expect('GET', '/Path');
5656
$httpBackend.expect('GET', '/Path/1');
5757
$httpBackend.expect('GET', '/Path/2/3');
58-
$httpBackend.expect('GET', '/Path/4/5/6');
58+
$httpBackend.expect('GET', '/Path/4/5');
59+
$httpBackend.expect('GET', '/Path/6/7/8');
5960

6061
R.get({});
6162
R.get({a:1});
6263
R.get({a:2, b:3});
63-
R.get({a:4, b:5, c:6});
64+
R.get({a:4, c:5});
65+
R.get({a:6, b:7, c:8});
6466
});
6567

6668

@@ -123,6 +125,27 @@ describe("resource", function() {
123125
});
124126

125127

128+
it('should build resource with action default param reading the value from instance', function() {
129+
$httpBackend.expect('POST', '/Customer/123').respond();
130+
var R = $resource('/Customer/:id', {}, {post: {method: 'POST', params: {id: '@id'}}});
131+
132+
var inst = new R({id:123});
133+
expect(inst.id).toBe(123);
134+
135+
inst.$post();
136+
});
137+
138+
139+
it('should handle multiple params with same name', function() {
140+
var R = $resource('/:id/:id');
141+
142+
$httpBackend.when('GET').respond('{}');
143+
$httpBackend.expect('GET', '/1/1');
144+
145+
R.get({id:1});
146+
});
147+
148+
126149
it("should create resource", function() {
127150
$httpBackend.expect('POST', '/CreditCard', '{"name":"misko"}').respond({id: 123, name: 'misko'});
128151

0 commit comments

Comments
 (0)