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

Commit

Permalink
fix($httpBackend): don't delete xhr.onreadystatechange otherwise Safa…
Browse files Browse the repository at this point in the history
…ri :-O
  • Loading branch information
IgorMinar committed Jan 3, 2014
1 parent bc492c0 commit 3d38fff
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
16 changes: 8 additions & 8 deletions src/ng/httpBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDoc
// response is in the cache. the promise api will ensure that to the app code the api is
// always async
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
// onreadystatechange might by called multiple times
// with readyState === 4 on mobile webkit caused by
// xhrs that are resolved while the app is in the background (see #5426).
//
// we must delete the property instead of setting it to undefined/null to make IE8 happy.
delete xhr.onreadystatechange;

// onreadystatechange might by called multiple times with readyState === 4 on mobile webkit caused by
// xhrs that are resolved while the app is in the background (see #5426).
// since calling completeRequest sets the `xhr` variable to null, we just check if it's not null before
// continuing
//
// we can't set xhr.onreadystatechange to undefined or delete it because that breaks IE8 (method=PATCH) and
// Safari respectively.
if (xhr && xhr.readyState == 4) {
var responseHeaders = null,
response = null;

Expand Down
6 changes: 4 additions & 2 deletions test/ng/httpBackendSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,16 @@ describe('$httpBackend', function() {
// onreadystatechange might by called multiple times
// with readyState === 4 on mobile webkit caused by
// xhrs that are resolved while the app is in the background (see #5426).
it('should remove onreadystatechange when it is called with readyState=4 to ignore multiple calls', function() {
it('should not process onreadystatechange callback with readyState == 4 more than once', function() {
$backend('GET', 'URL', null, callback);
xhr = MockXhr.$$lastInstance;

xhr.status = 200;
xhr.readyState = 4;
xhr.onreadystatechange();
expect(xhr.onreadystatechange).toBeUndefined();
xhr.onreadystatechange();

expect(callback).toHaveBeenCalledOnce();
});

it('should set only the requested headers', function() {
Expand Down

0 comments on commit 3d38fff

Please sign in to comment.