From 9c8c34b63b9035849cddbf8fa92faf45f2ba8ac6 Mon Sep 17 00:00:00 2001 From: Fredrik Bonander Date: Fri, 21 Sep 2012 13:39:45 +0200 Subject: [PATCH 1/2] fix($resource) Route constructor, updated RegExp Update RegExp to allow urlParams with out leading slash (/). - Will allow reoucese to be loaded from a relative path Example: var R = $resource(':path'); R.get({ path : 'data.json' }); Example usage: Load resources in applications not using webserver, ie local webapp in on a tablet. --- src/ngResource/resource.js | 2 +- test/ngResource/resourceSpec.js | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/ngResource/resource.js b/src/ngResource/resource.js index ab5dc52a4a4c..bcc9c3d51378 100644 --- a/src/ngResource/resource.js +++ b/src/ngResource/resource.js @@ -269,7 +269,7 @@ angular.module('ngResource', ['ng']). this.defaults = defaults || {}; var urlParams = this.urlParams = {}; forEach(template.split(/\W/), function(param){ - if (param && template.match(new RegExp("[^\\\\]:" + param + "\\W"))) { + if (param && (new RegExp("((\\w|\\/|^)(?!\\\\:" + param + ")):" + param + "\\W")).test(template)) { urlParams[param] = true; } }); diff --git a/test/ngResource/resourceSpec.js b/test/ngResource/resourceSpec.js index dc837f8030b2..2fc8f81fc315 100644 --- a/test/ngResource/resourceSpec.js +++ b/test/ngResource/resourceSpec.js @@ -104,6 +104,12 @@ describe("resource", function() { R.get({a: 'doh@fo o', ':bar': '$baz@1', '!do&h': 'g=a h'}); }); + it('should allow relative paths in resource url', function () { + var R = $resource(':a'); + $httpBackend.expect('GET', 'data.json').respond('{}'); + R.get({ a: 'data.json' }); + }); + it('should encode & in url params', function() { var R = $resource('/Path/:a'); From 821d98aace07f95413f0bc12075fefc267bd716e Mon Sep 17 00:00:00 2001 From: Fredrik Bonander Date: Wed, 23 Jan 2013 10:53:49 +0100 Subject: [PATCH 2/2] fixed regexp to support relative paths --- src/ngResource/resource.js | 2 +- test/ngResource/resourceSpec.js | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/ngResource/resource.js b/src/ngResource/resource.js index 549226a2c7eb..79114bc68760 100644 --- a/src/ngResource/resource.js +++ b/src/ngResource/resource.js @@ -290,7 +290,7 @@ angular.module('ngResource', ['ng']). this.defaults = defaults || {}; var urlParams = this.urlParams = {}; forEach(template.split(/\W/), function(param){ - if (param && (new RegExp("((\\w|\\/|^)(?!\\\\:" + param + ")):" + param + "\\W")).test(template)) { + if (param && (new RegExp("(^|[^\\\\]):" + param + "\\W").test(template))) { urlParams[param] = true; } }); diff --git a/test/ngResource/resourceSpec.js b/test/ngResource/resourceSpec.js index cf3b886bbca2..d936b33a3ead 100644 --- a/test/ngResource/resourceSpec.js +++ b/test/ngResource/resourceSpec.js @@ -130,9 +130,15 @@ describe("resource", function() { }); it('should allow relative paths in resource url', function () { - var R = $resource(':a'); + var R = $resource(':relativePath'); $httpBackend.expect('GET', 'data.json').respond('{}'); - R.get({ a: 'data.json' }); + R.get({ relativePath: 'data.json' }); + }); + + it('should handle + in url params', function () { + var R = $resource('/api/myapp/:myresource?from=:from&to=:to&histlen=:histlen'); + $httpBackend.expect('GET', '/api/myapp/pear+apple?from=2012-04-01&to=2012-04-29&histlen=3').respond('{}'); + R.get({ myresource: 'pear+apple', from : '2012-04-01', to : '2012-04-29', histlen : 3 }); });