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

Commit 05661fa

Browse files
committed
fix($resource): copy provider configuration conventions from $httpBackend
First, this now uses a flat object configuration, similar to `$httpBackend`. This should make configuring this provider much more familiar. This adds a fourth optional argument to the `$resource()` constructor, supporting overriding global `$resourceProvider` configuration. Now, both of these ways of configuring this is supported: app.config(function($resourceProvider) { $resourceProvider.defaults.stripTrailingSlashes = false; }); or per instance: var CreditCard = $resource('/some/:url/', ..., ..., { stripTrailingSlashes: false });
1 parent 7a82951 commit 05661fa

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

src/ngResource/resource.js

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ function shallowClearAndCopy(src, dst) {
8282
* <pre>
8383
app.config(['$resourceProvider', function ($resourceProvider) {
8484
// Don't strip trailing slashes from calculated URLs
85-
$resourceProvider.setStripTrailingSlashes(false);
85+
$resourceProvider.defaults.stripTrailingSlashes = false;
8686
}]);
8787
</pre>
8888
*
@@ -153,6 +153,14 @@ function shallowClearAndCopy(src, dst) {
153153
* - **`interceptor`** - `{Object=}` - The interceptor object has two optional methods -
154154
* `response` and `responseError`. Both `response` and `responseError` interceptors get called
155155
* with `http response` object. See {@link ng.$http $http interceptors}.
156+
*
157+
* @param {Object} options Hash with custom settings that should extend the
158+
* default `$resourceProvider` behavior. The only supported option is
159+
*
160+
* Where:
161+
*
162+
* - **`stripTrailingSlashes`** – {boolean} – If true then the trailing
163+
* slashes from any calculated URL will be stripped. (Defaults to true.)
156164
*
157165
* @returns {Object} A resource "class" object with methods for the default set of resource actions
158166
* optionally extended with custom `actions`. The default set contains these actions:
@@ -316,17 +324,13 @@ function shallowClearAndCopy(src, dst) {
316324
*/
317325
angular.module('ngResource', ['ng']).
318326
provider('$resource', function () {
319-
var _self = this;
327+
var provider = this;
320328

321-
this.config = {
329+
this.defaults = {
322330
// Strip slashes by default
323331
stripTrailingSlashes: true,
324332
};
325333

326-
this.setStripTrailingSlashes = function (value) {
327-
_self.config.stripTrailingSlashes = value;
328-
};
329-
330334
this.$get = ['$http', '$q', function($http, $q) {
331335

332336
var DEFAULT_ACTIONS = {
@@ -383,7 +387,7 @@ angular.module('ngResource', ['ng']).
383387

384388
function Route(template, defaults) {
385389
this.template = template;
386-
this.defaults = defaults || {};
390+
this.defaults = extend({}, provider.defaults, defaults);
387391
this.urlParams = {};
388392
}
389393

@@ -425,7 +429,7 @@ angular.module('ngResource', ['ng']).
425429
});
426430

427431
// strip trailing slashes and set the url (unless this behaviour is specifically disabled)
428-
if (_self.config.stripTrailingSlashes) {
432+
if (self.defaults.stripTrailingSlashes) {
429433
url = url.replace(/\/+$/, '');
430434
}
431435

@@ -447,8 +451,8 @@ angular.module('ngResource', ['ng']).
447451
};
448452

449453

450-
function resourceFactory(url, paramDefaults, actions) {
451-
var route = new Route(url);
454+
function resourceFactory(url, paramDefaults, actions, options) {
455+
var route = new Route(url, options);
452456

453457
actions = extend({}, DEFAULT_ACTIONS, actions);
454458

test/ngResource/resourceSpec.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,14 +182,21 @@ describe("resource", function() {
182182
});
183183

184184

185-
it('should not strip trailing slashes if configured not to', function() {
186-
// Set the new behaviour
187-
resourceProvider.setStripTrailingSlashes(false);
185+
it('should not strip the trailing slashes from URLs if configured', function() {
186+
// Set the new behaviour for all new resources created by overriding the
187+
// provider configuration
188+
resourceProvider.defaults.stripTrailingSlashes = false;
188189

189190
var R = $resource('http://localhost:8080/Path/:a/');
190191

191192
$httpBackend.expect('GET', 'http://localhost:8080/Path/foo/').respond();
192193
R.get({a: 'foo'});
194+
195+
// Specific instances of $resource can still override the provider's default
196+
R = $resource('http://localhost:8080/Path/:a/', {}, {}, {stripTrailingSlashes: true});
197+
198+
$httpBackend.expect('GET', 'http://localhost:8080/Path/foo').respond();
199+
R.get({a: 'foo'});
193200
});
194201

195202

0 commit comments

Comments
 (0)