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

Commit 2955bbf

Browse files
committed
fix(ngResource): canceling XHR request using promise
1 parent f95bc42 commit 2955bbf

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

src/ngResource/resource.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -568,8 +568,10 @@ angular.module('ngResource', ['ng']).
568568
undefined;
569569

570570
forEach(action, function(value, key) {
571-
if (key != 'params' && key != 'isArray' && key != 'interceptor') {
571+
if (key != 'params' && key != 'isArray' && key != 'interceptor' && key != 'timeout') {
572572
httpConfig[key] = copy(value);
573+
} else if (key == 'timeout') {
574+
httpConfig[key] = value;
573575
}
574576
});
575577

test/ngResource/resourceSpec.js

+33-1
Original file line numberDiff line numberDiff line change
@@ -1308,7 +1308,7 @@ describe("resource", function() {
13081308
});
13091309

13101310
describe('resource', function() {
1311-
var $httpBackend, $resource;
1311+
var $httpBackend, $resource, $q;
13121312

13131313
beforeEach(module(function($exceptionHandlerProvider) {
13141314
$exceptionHandlerProvider.mode('log');
@@ -1319,6 +1319,7 @@ describe('resource', function() {
13191319
beforeEach(inject(function($injector) {
13201320
$httpBackend = $injector.get('$httpBackend');
13211321
$resource = $injector.get('$resource');
1322+
$q = $injector.get('$q');
13221323
}));
13231324

13241325

@@ -1356,5 +1357,36 @@ describe('resource', function() {
13561357
);
13571358
});
13581359

1360+
it('If timeout promise is resolved, cancel the request', function() {
1361+
var canceler = $q.defer();
1362+
1363+
$httpBackend.when('GET', '/CreditCard').respond({data: '123'});
1364+
1365+
var CreditCard = $resource('/CreditCard', {}, {
1366+
query: {
1367+
method: 'GET',
1368+
timeout: canceler.promise
1369+
}
1370+
});
1371+
1372+
CreditCard.query();
1373+
1374+
canceler.resolve();
1375+
expect(function() { $httpBackend.flush();}).toThrow(new Error("No pending request to flush !"));
1376+
1377+
canceler = $q.defer();
1378+
CreditCard = $resource('/CreditCard', {}, {
1379+
query: {
1380+
method: 'GET',
1381+
timeout: canceler.promise
1382+
}
1383+
});
1384+
1385+
CreditCard.query();
1386+
expect(function() { $httpBackend.flush();}).not.toThrow(new Error("No pending request to flush !"));
1387+
1388+
1389+
});
1390+
13591391

13601392
});

0 commit comments

Comments
 (0)