Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use XMLHttpRequest directly #75

Merged
merged 1 commit into from
Feb 13, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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