From 721d13548f26a1aa10f121dbc2ac6b1a274dfe4e Mon Sep 17 00:00:00 2001 From: petrovalex Date: Thu, 10 May 2012 02:43:37 +0300 Subject: [PATCH 1/3] fix issue #782 --- src/ngResource/resource.js | 2 +- test/ngResource/resourceSpec.js | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/ngResource/resource.js b/src/ngResource/resource.js index d19a1d6a6aee..d1766a160ac7 100644 --- a/src/ngResource/resource.js +++ b/src/ngResource/resource.js @@ -284,7 +284,7 @@ angular.module('ngResource', ['ng']). params = params || {}; forEach(this.urlParams, function(_, urlParam){ encodedVal = encodeUriSegment(params[urlParam] || self.defaults[urlParam] || ""); - url = url.replace(new RegExp(":" + urlParam + "(\\W)"), encodedVal + "$1"); + url = url.replace(new RegExp(":" + urlParam + "(\\W)", "g"), encodedVal + "$1"); }); url = url.replace(/\/?#$/, ''); var query = []; diff --git a/test/ngResource/resourceSpec.js b/test/ngResource/resourceSpec.js index 2981732c9683..d91cf9b68885 100644 --- a/test/ngResource/resourceSpec.js +++ b/test/ngResource/resourceSpec.js @@ -116,6 +116,16 @@ describe("resource", function() { }); + it('should handle multiple params with same name', function() { + var R = $resource('/:id/:id'); + + $httpBackend.when('GET').respond('{}'); + $httpBackend.expect('GET', '/1/1'); + + R.get({id:1}); + }); + + it("should create resource", function() { $httpBackend.expect('POST', '/CreditCard', '{"name":"misko"}').respond({id: 123, name: 'misko'}); From a9311544a8e2cf9370914a5a23e0c75b86ceb108 Mon Sep 17 00:00:00 2001 From: petrovalex Date: Fri, 11 May 2012 01:46:56 +0300 Subject: [PATCH 2/3] covered the situation when undefined parameters are not the end of the url --- src/ngResource/resource.js | 9 +++++++-- test/ngResource/resourceSpec.js | 6 ++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/ngResource/resource.js b/src/ngResource/resource.js index d1766a160ac7..29cd2a63295f 100644 --- a/src/ngResource/resource.js +++ b/src/ngResource/resource.js @@ -279,12 +279,17 @@ angular.module('ngResource', ['ng']). url: function(params) { var self = this, url = this.template, + val, encodedVal; params = params || {}; forEach(this.urlParams, function(_, urlParam){ - encodedVal = encodeUriSegment(params[urlParam] || self.defaults[urlParam] || ""); - url = url.replace(new RegExp(":" + urlParam + "(\\W)", "g"), encodedVal + "$1"); + if (val = (params[urlParam] || self.defaults[urlParam])) { + encodedVal = encodeUriSegment(val); + url = url.replace(new RegExp(":" + urlParam + "(\\W)", "g"), encodedVal + "$1"); + } else { + url = url.replace(new RegExp("/?:" + urlParam + "(\\W)", "g"), '$1'); + } }); url = url.replace(/\/?#$/, ''); var query = []; diff --git a/test/ngResource/resourceSpec.js b/test/ngResource/resourceSpec.js index d91cf9b68885..41f1f1f12670 100644 --- a/test/ngResource/resourceSpec.js +++ b/test/ngResource/resourceSpec.js @@ -48,12 +48,14 @@ describe("resource", function() { $httpBackend.expect('GET', '/Path'); $httpBackend.expect('GET', '/Path/1'); $httpBackend.expect('GET', '/Path/2/3'); - $httpBackend.expect('GET', '/Path/4/5/6'); + $httpBackend.expect('GET', '/Path/4/5'); + $httpBackend.expect('GET', '/Path/6/7/8'); R.get({}); R.get({a:1}); R.get({a:2, b:3}); - R.get({a:4, b:5, c:6}); + R.get({a:4, c:5}); + R.get({a:6, b:7, c:8}); }); From 5e8ead2de1457d0cc4dbac507be2ec8d1962620c Mon Sep 17 00:00:00 2001 From: petrovalex Date: Fri, 11 May 2012 23:14:59 +0300 Subject: [PATCH 3/3] - fix related to the issue #875 --- src/ngResource/resource.js | 5 +++-- test/ngResource/resourceSpec.js | 11 +++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/ngResource/resource.js b/src/ngResource/resource.js index 29cd2a63295f..57aa5148d4de 100644 --- a/src/ngResource/resource.js +++ b/src/ngResource/resource.js @@ -310,8 +310,9 @@ angular.module('ngResource', ['ng']). actions = extend({}, DEFAULT_ACTIONS, actions); - function extractParams(data){ + function extractParams(data, actionParams){ var ids = {}; + paramDefaults = extend(paramDefaults, actionParams); forEach(paramDefaults || {}, function(value, key){ ids[key] = value.charAt && value.charAt(0) == '@' ? getter(data, value.substr(1)) : value; }); @@ -366,7 +367,7 @@ angular.module('ngResource', ['ng']). var value = this instanceof Resource ? this : (action.isArray ? [] : new Resource(data)); $http({ method: action.method, - url: route.url(extend({}, extractParams(data), action.params || {}, params)), + url: route.url(extend({}, extractParams(data, action.params || {}), params)), data: data }).then(function(response) { var data = response.data; diff --git a/test/ngResource/resourceSpec.js b/test/ngResource/resourceSpec.js index 41f1f1f12670..2fea357a4521 100644 --- a/test/ngResource/resourceSpec.js +++ b/test/ngResource/resourceSpec.js @@ -118,6 +118,17 @@ describe("resource", function() { }); + it('should build resource with action default param reading the value from instance', function() { + $httpBackend.expect('POST', '/Customer/123').respond(); + var R = $resource('/Customer/:id', {}, {post: {method: 'POST', params: {id: '@id'}}}); + + var inst = new R({id:123}); + expect(inst.id).toBe(123); + + inst.$post(); + }); + + it('should handle multiple params with same name', function() { var R = $resource('/:id/:id');