Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions src/ngResource/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,10 @@ angular.module('ngResource', ['ng']).
forEach = angular.forEach,
extend = angular.extend,
copy = angular.copy,
isArray = angular.isArray,
isDefined = angular.isDefined,
isFunction = angular.isFunction,
isNumber = angular.isNumber,
encodeUriQuery = angular.$$encodeUriQuery,
encodeUriSegment = angular.$$encodeUriSegment;

Expand Down Expand Up @@ -561,7 +564,7 @@ angular.module('ngResource', ['ng']).
params = params || {};
forEach(self.urlParams, function(paramInfo, urlParam) {
val = params.hasOwnProperty(urlParam) ? params[urlParam] : self.defaults[urlParam];
if (angular.isDefined(val) && val !== null) {
if (isDefined(val) && val !== null) {
if (paramInfo.isQueryParamValue) {
encodedVal = encodeUriQuery(val, true);
} else {
Expand Down Expand Up @@ -639,11 +642,10 @@ angular.module('ngResource', ['ng']).
forEach(actions, function(action, name) {
var hasBody = /^(POST|PUT|PATCH)$/i.test(action.method);
var numericTimeout = action.timeout;
var cancellable = angular.isDefined(action.cancellable) ? action.cancellable :
(options && angular.isDefined(options.cancellable)) ? options.cancellable :
provider.defaults.cancellable;
var cancellable = isDefined(action.cancellable) ?
action.cancellable : route.defaults.cancellable;

if (numericTimeout && !angular.isNumber(numericTimeout)) {
if (numericTimeout && !isNumber(numericTimeout)) {
$log.debug('ngResource:\n' +
' Only numeric values are allowed as `timeout`.\n' +
' Promises are not supported in $resource, because the same value would ' +
Expand Down Expand Up @@ -736,11 +738,11 @@ angular.module('ngResource', ['ng']).

if (data) {
// Need to convert action.isArray to boolean in case it is undefined
if (angular.isArray(data) !== (!!action.isArray)) {
if (isArray(data) !== (!!action.isArray)) {
throw $resourceMinErr('badcfg',
'Error in resource configuration for action `{0}`. Expected response to ' +
'contain an {1} but got an {2} (Request: {3} {4})', name, action.isArray ? 'array' : 'object',
angular.isArray(data) ? 'array' : 'object', httpConfig.method, httpConfig.url);
isArray(data) ? 'array' : 'object', httpConfig.method, httpConfig.url);
}
if (action.isArray) {
value.length = 0;
Expand Down Expand Up @@ -768,7 +770,7 @@ angular.module('ngResource', ['ng']).
promise = promise['finally'](function() {
value.$resolved = true;
if (!isInstanceCall && cancellable) {
value.$cancelRequest = angular.noop;
value.$cancelRequest = noop;
$timeout.cancel(numericTimeoutPromise);
timeoutDeferred = numericTimeoutPromise = httpConfig.timeout = null;
}
Expand Down Expand Up @@ -825,7 +827,8 @@ angular.module('ngResource', ['ng']).
});

Resource.bind = function(additionalParamDefaults) {
return resourceFactory(url, extend({}, paramDefaults, additionalParamDefaults), actions);
var extendedParamDefaults = extend({}, paramDefaults, additionalParamDefaults);
return resourceFactory(url, extendedParamDefaults, actions, options);
};

return Resource;
Expand Down
21 changes: 18 additions & 3 deletions test/ngResource/resourceSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -636,10 +636,25 @@ describe('basic usage', function() {

it('should bind default parameters', function() {
$httpBackend.expect('GET', '/CreditCard/123.visa?minimum=0.05').respond({id: 123});
var Visa = CreditCard.bind({verb:'.visa', minimum:0.05});
var visa = Visa.get({id:123});
var Visa = CreditCard.bind({verb: '.visa', minimum: 0.05});
var visa = Visa.get({id: 123});
$httpBackend.flush();
expect(visa).toEqualData({id:123});
expect(visa).toEqualData({id: 123});
});


it('should pass all original arguments when binding default params', function() {
$httpBackend.expect('GET', '/CancellableCreditCard/123.visa').respond({id: 123});

var CancellableCreditCard = $resource('/CancellableCreditCard/:id:verb', {},
{charge: {method: 'POST'}}, {cancellable: true});
var CancellableVisa = CancellableCreditCard.bind({verb: '.visa'});
var visa = CancellableVisa.get({id: 123});

$httpBackend.flush();

expect(visa.$charge).toEqual(jasmine.any(Function));
expect(visa.$cancelRequest).toEqual(jasmine.any(Function));
});


Expand Down