From 53061363c7aa1ab9085273d269c6f04ac2162336 Mon Sep 17 00:00:00 2001 From: Glenn Goodrich Date: Mon, 13 May 2013 14:16:03 +0100 Subject: feat($resource): collapse empty suffix parameters correctly Previously only repeated `/` delimiters were collapsed into a single `/`. Now, the sequence `/.` at the end of the template, i.e. only followed by a sequence of word characters, is collapsed into a single `.`. This makes it easier to support suffixes on resource URLs. For example, given a resource template of `/some/path/:id.:format`, if the `:id` is `""` but format `"json"` then the URL is now `/some/path.json`, rather than `/some/path/.json`. BREAKING CHANGE: A `/` followed by a `.`, in the last segment of the URL template is now collapsed into a single `.` delimiter. For example: `users/.json` will become `users.json`. If your server relied upon this sequence then it will no longer work. In this case you can now escape the `/.` sequence with `/\.` --- src/ngResource/resource.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'src/ngResource/resource.js') diff --git a/src/ngResource/resource.js b/src/ngResource/resource.js index 3eb4e79e..a2e915e8 100644 --- a/src/ngResource/resource.js +++ b/src/ngResource/resource.js @@ -34,6 +34,13 @@ * `http://example.com:8080/api`), you'll need to escape the colon character before the port * number, like this: `$resource('http://example.com\\:8080/api')`. * + * If you are using a url with a suffix, just add the suffix, like this: + * `$resource('http://example.com/resource.json')` or `$resource('http://example.com/:id.json') + * or even `$resource('http://example.com/resource/:resource_id.:format')` + * If the parameter before the suffix is empty, :resource_id in this case, then the `/.` will be + * collapsed down to a single `.`. If you need this sequence to appear and not collapse then you + * can escape it with `/\.`. + * * @param {Object=} paramDefaults Default values for `url` parameters. These can be overridden in * `actions` methods. If any of the parameter value is a function, it will be executed every time * when a param value needs to be obtained for a request (unless the param was overridden). @@ -356,7 +363,13 @@ angular.module('ngResource', ['ng']). }); // strip trailing slashes and set the url - config.url = url.replace(/\/+$/, ''); + url = url.replace(/\/+$/, ''); + // then replace collapse `/.` if found in the last URL path segment before the query + // E.g. `http://url.com/id./format?q=x` becomes `http://url.com/id.format?q=x` + url = url.replace(/\/\.(?=\w+($|\?))/, '.'); + // replace escaped `/\.` with `/.` + config.url = url.replace(/\/\\\./, '/.'); + // set params - delegate param encoding to $http forEach(params, function(value, key){ -- cgit v1.2.3