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

Commit fd5baea

Browse files
committed
canceling XHR request using promises fixed
1 parent c211e7a commit fd5baea

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

src/ngResource/resource.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,8 +560,10 @@ angular.module('ngResource', ['ng']).
560560
undefined;
561561

562562
forEach(action, function(value, key) {
563-
if (key != 'params' && key != 'isArray' && key != 'interceptor') {
563+
if (key != 'params' && key != 'isArray' && key != 'interceptor' && key != 'timeout') {
564564
httpConfig[key] = copy(value);
565+
} else if (key == 'timeout') {
566+
httpConfig[key] = value;
565567
}
566568
});
567569

test/ngResource/resourceSpec.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1295,7 +1295,7 @@ describe("resource", function() {
12951295
});
12961296

12971297
describe('resource', function() {
1298-
var $httpBackend, $resource;
1298+
var $httpBackend, $resource, $q;
12991299

13001300
beforeEach(module(function($exceptionHandlerProvider) {
13011301
$exceptionHandlerProvider.mode('log');
@@ -1306,6 +1306,7 @@ describe('resource', function() {
13061306
beforeEach(inject(function($injector) {
13071307
$httpBackend = $injector.get('$httpBackend');
13081308
$resource = $injector.get('$resource');
1309+
$q = $injector.get('$q');
13091310
}));
13101311

13111312

@@ -1343,5 +1344,36 @@ describe('resource', function() {
13431344
);
13441345
});
13451346

1347+
it('If timeout promise is resolved, cancel the request', function() {
1348+
var canceler = $q.defer();
1349+
1350+
$httpBackend.when('GET', '/CreditCard').respond({data: '123'});
1351+
1352+
var CreditCard = $resource('/CreditCard', {}, {
1353+
query: {
1354+
method: 'GET',
1355+
timeout: canceler.promise
1356+
}
1357+
});
1358+
1359+
CreditCard.query();
1360+
1361+
canceler.resolve();
1362+
expect(function() { $httpBackend.flush();}).toThrow(new Error("No pending request to flush !"));
1363+
1364+
canceler = $q.defer();
1365+
CreditCard = $resource('/CreditCard', {}, {
1366+
query: {
1367+
method: 'GET',
1368+
timeout: canceler.promise
1369+
}
1370+
});
1371+
1372+
CreditCard.query();
1373+
expect(function() { $httpBackend.flush();}).not.toThrow(new Error("No pending request to flush !"));
1374+
1375+
1376+
});
1377+
13461378

13471379
});

0 commit comments

Comments
 (0)