Skip to content

Commit

Permalink
Merge pull request #75 from arv/xhr
Browse files Browse the repository at this point in the history
Use XMLHttpRequest directly
  • Loading branch information
Steve Orvell committed Feb 13, 2013
2 parents 2ebc5e4 + 12350f0 commit e997173
Showing 1 changed file with 8 additions and 20 deletions.
28 changes: 8 additions & 20 deletions components/g-xhr.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,10 @@
</template>
<script>
this.component({
getTransport: function() {
try {
return new XMLHttpRequest();
} catch (e) {}
try {
return new ActiveXObject('Msxml2.XMLHTTP');
} catch (e) {}
try {
return new ActiveXObject('Microsoft.XMLHTTP');
} catch (e) {}
return false;
},
makeReadyStateHandler: function(inXhr, inCallback) {
inXhr.onreadystatechange = function() {
if (inXhr.readyState == 4) {
inCallback && inCallback.apply(null, [inXhr.responseText, inXhr]);
inCallback && inCallback.call(null, inXhr.responseText, inXhr);
}
};
},
Expand All @@ -45,7 +33,7 @@
for (var n in inParams) {
var v = inParams[n];
n = encodeURIComponent(n);
r.push(v === undefined || v === null ? n : (n + '=' + encodeURIComponent(v)));
r.push(v == null ? n : (n + '=' + encodeURIComponent(v)));
}
return r.join('&');
},
Expand All @@ -65,22 +53,22 @@
* @returns XHR object.
*/
request: function(inOptions) {
var transport = this.getTransport();
var xhr = new XMLHttpRequest();
var url = inOptions.url;
var method = inOptions.method || 'GET';
var async = !inOptions.sync;
var params = this.toQueryString(inOptions.params);
if (params && method == 'GET') {
url += (url.indexOf('?') > 0 ? '&' : '?') + params;
}
transport.open(method, url, async);
this.makeReadyStateHandler(transport, inOptions.callback);
xhr.open(method, url, async);
this.makeReadyStateHandler(xhr, inOptions.callback);
this.setRequestHeaders(inOptions.headers);
transport.send(method == 'POST' ? (inOptions.body || params) : null);
xhr.send(method == 'POST' ? (inOptions.body || params) : null);
if (!async) {
transport.onreadystatechange(transport);
xhr.onreadystatechange(xhr);
}
return transport;
return xhr;
}
}
});
Expand Down

0 comments on commit e997173

Please sign in to comment.