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

Commit 0b5ecc6

Browse files
fix($resource): still use the cancellable value if invalid timeout value
We log a deprecation message if `timeout` contains an invalid value. Now we also use the `callable` value if provided.
1 parent b183eae commit 0b5ecc6

File tree

2 files changed

+40
-12
lines changed

2 files changed

+40
-12
lines changed

src/ngResource/resource.js

+11-12
Original file line numberDiff line numberDiff line change
@@ -576,18 +576,17 @@ angular.module('ngResource', ['ng']).
576576

577577
forEach(actions, function(action, name) {
578578
var hasBody = /^(POST|PUT|PATCH)$/i.test(action.method);
579-
var cancellable;
580-
581-
if (angular.isNumber(action.timeout)) {
582-
cancellable = false;
583-
} else if (action.timeout) {
584-
$log.debug('ngResource:\n' +
585-
' Only numeric values are allowed as `timeout`.\n' +
586-
' Promises are not supported in $resource, because the same value has to ' +
587-
'be re-used for multiple requests. If you are looking for a way to cancel ' +
588-
'requests, you should use the `cancellable` option.');
589-
delete action.timeout;
590-
} else {
579+
var cancellable = false;
580+
581+
if (!angular.isNumber(action.timeout)) {
582+
if (action.timeout) {
583+
$log.debug('ngResource:\n' +
584+
' Only numeric values are allowed as `timeout`.\n' +
585+
' Promises are not supported in $resource, because the same value has to ' +
586+
'be re-used for multiple requests. If you are looking for a way to cancel ' +
587+
'requests, you should use the `cancellable` option.');
588+
delete action.timeout;
589+
}
591590
cancellable = angular.isDefined(action.cancellable) ? action.cancellable :
592591
(options && angular.isDefined(options.cancellable)) ? options.cancellable :
593592
provider.defaults.cancellable;

test/ngResource/resourceSpec.js

+29
Original file line numberDiff line numberDiff line change
@@ -1427,6 +1427,35 @@ describe('cancelling requests', function() {
14271427
})
14281428
);
14291429

1430+
it('should use `cancellable` value if passed a non-numeric `timeout` in an action',
1431+
inject(function($log, $q) {
1432+
spyOn($log, 'debug');
1433+
$httpBackend.whenGET('/CreditCard').respond({});
1434+
1435+
var CreditCard = $resource('/CreditCard', {}, {
1436+
get: {
1437+
method: 'GET',
1438+
timeout: $q.defer().promise,
1439+
cancellable: true
1440+
}
1441+
});
1442+
1443+
var creditCard = CreditCard.get();
1444+
expect(creditCard.$cancelRequest).toBeDefined();
1445+
expect(httpSpy.calls[0].args[0].timeout.then).toBeDefined();
1446+
1447+
// $httpBackend.flush();
1448+
1449+
// expect(httpSpy).toHaveBeenCalledOnce();
1450+
// expect(httpSpy.calls[0].args[0].timeout).toBe(jasmine.any());
1451+
// expect($log.debug).toHaveBeenCalledOnceWith('ngResource:\n' +
1452+
// ' Only numeric values are allowed as `timeout`.\n' +
1453+
// ' Promises are not supported in $resource, because the same value has to ' +
1454+
// 'be re-used for multiple requests. If you are looking for a way to cancel ' +
1455+
// 'requests, you should use the `cancellable` option.');
1456+
})
1457+
);
1458+
14301459
it('should not create a `$cancelRequest` method for instance calls', function() {
14311460
$httpBackend.whenPOST('/CreditCard').respond({});
14321461

0 commit comments

Comments
 (0)