Permalink
Browse files

[JX.Request] Expose transport XHR object

Summary:
There are tons of properties and methods people might want to call on the XHR
object in edge cases, which wouldn't make sense to expose through APIs directly
on JX.Request. For example, if you need to do a cross-domain request, you need
to call ##xport.withCredentials = "true"##. Sure, we could add a
##setWithCredentials## API to JX.Request, but it's a pretty obscure thing to
need to do (right now at least).

Test Plan:
none yet

Reviewed By: epriestley
Reviewers: epriestley, tomo, aran
Commenters: tomo, aran, nikolay
CC: aran, tomo, mroch, epriestley, nikolay
Differential Revision: 345
  • Loading branch information...
mroch committed May 25, 2011
1 parent 7ac64cd commit 140e036937954e05a171fe60a025969e508d98e4
Showing with 27 additions and 10 deletions.
  1. +27 −10 src/lib/Request.js
View
@@ -24,27 +24,42 @@ JX.install('Request', {
_xhrkey : null,
_transport : null,
+ _sent : false,
_finished : false,
_block : null,
_data : null,
- send : function() {
- var xport = null;
-
- try {
+ getTransport : function() {
+ var xport = this._transport;
+ if (!xport) {
try {
- xport = new XMLHttpRequest();
+ try {
+ xport = new XMLHttpRequest();
+ } catch (x) {
+ xport = new ActiveXObject("Msxml2.XMLHTTP");
+ }
} catch (x) {
- xport = new ActiveXObject("Msxml2.XMLHTTP");
+ xport = new ActiveXObject("Microsoft.XMLHTTP");
+ }
+ this._transport = xport;
+ }
+ return xport;
+ },
+
+ send : function() {
+ if (this._sent) {
+ if (__DEV__) {
+ throw new Error(
+ 'JX.Request.send(): '+
+ 'attempting to send a Request that has already been sent.');
}
- } catch (x) {
- xport = new ActiveXObject("Microsoft.XMLHTTP");
+ return;
}
- this._transport = xport;
this._xhrkey = JX.Request._xhr.length;
JX.Request._xhr.push(this);
+ var xport = this.getTransport();
xport.onreadystatechange = JX.bind(this, this._onreadystatechange);
var list_of_pairs = this._data || [];
@@ -103,14 +118,16 @@ JX.install('Request', {
} else {
xport.send(null);
}
+
+ this._sent = true;
},
abort : function() {
this._cleanup();
},
_onreadystatechange : function() {
- var xport = this._transport;
+ var xport = this.getTransport();
try {
if (this._finished) {
return;

0 comments on commit 140e036

Please sign in to comment.