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

Commit

Permalink
fix: wrap XMLHttpRequest when we cant patch protos
Browse files Browse the repository at this point in the history
  • Loading branch information
btford committed Apr 17, 2014
1 parent 3791431 commit 76de58e
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
50 changes: 50 additions & 0 deletions test/zone.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,56 @@ describe('Zone.patch', function () {

});

describe('XMLHttpRequest', function () {

it('should work with onreadystatechange', function () {
var flag = false,
hasParent;

runs(function () {
var req = new XMLHttpRequest();
req.onreadystatechange = function () {
hasParent = !!window.zone.parent;
flag = true;
};
req.open('get', '/', true);
req.send();
});

waitsFor(function() {
return flag;
}, 'HTTP request to resolve', 100);

runs(function() {
expect(hasParent).toBe(true);
});
});

it('should work with onprogress', function () {
var flag = false,
hasParent;

runs(function () {
var req = new XMLHttpRequest();
req.onprogress = function () {
hasParent = !!window.zone.parent;
flag = true;
};
req.open('get', '/', true);
req.send();
});

waitsFor(function() {
return flag;
}, 'HTTP request to resolve', 100);

runs(function() {
expect(hasParent).toBe(true);
});
});
});


describe('hooks', function () {

beforeEach(function () {
Expand Down
33 changes: 33 additions & 0 deletions zone.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ Zone.patch = function patch () {
Zone.patchViaPropertyDescriptor();
} else {
Zone.patchViaCapturingAllTheEvents();
Zone.patchClass('XMLHttpRequest');
}

// patch promises
Expand Down Expand Up @@ -304,6 +305,38 @@ Zone.patchViaCapturingAllTheEvents = function () {
});
};

// TODO: wrap some native API
Zone.patchClass = function (className) {
var OriginalClass = window[className];
window[className] = function () {
this._o = new OriginalClass();
};

var instance = (new OriginalClass());

var prop;
for (prop in instance) {
(function (prop) {
if (typeof instance[prop] === 'function') {
window[className].prototype[prop] = function () {
return this._o[prop].apply(this._o, arguments);
};
} else {
Object.defineProperty(window[className].prototype, prop, {
set: function (fn) {
if (typeof fn === 'function') {
this._o[prop] = zone.bind(fn);
}
},
get: function () {
return this._o[prop];
}
});
}
}(prop));
};
};

Zone.eventNames = 'copy cut paste abort blur focus canplay canplaythrough change click contextmenu dblclick drag dragend dragenter dragleave dragover dragstart drop durationchange emptied ended input invalid keydown keypress keyup load loadeddata loadedmetadata loadstart mousedown mouseenter mouseleave mousemove mouseout mouseover mouseup pause play playing progress ratechange reset scroll seeked seeking select show stalled submit suspend timeupdate volumechange waiting mozfullscreenchange mozfullscreenerror mozpointerlockchange mozpointerlockerror error'.split(' ');

Zone.init = function init () {
Expand Down

0 comments on commit 76de58e

Please sign in to comment.