Skip to content

Instantly share code, notes, and snippets.

@megoth
Created April 19, 2012 14:00
Show Gist options
  • Save megoth/2421167 to your computer and use it in GitHub Desktop.
Save megoth/2421167 to your computer and use it in GitHub Desktop.
graphite.loader.xhr
/*global graphite*/
(function (graphite, loader, proxy) {
"use strict";
var xhr = function (options) {
this.options = graphite.extend({
"asynchronous": true,
"method": "GET",
"uri": window.location
}, options);
this.req = new XMLHttpRequest();
if (this.options.success) {
send.call(this, this.options.success);
}
};
xhr.prototype = {
abort: function () {
this.req.abort();
},
send: send
};
function send(callback) {
var obj = this;
this.req.open(this.options.method, this.options.uri, this.options.asynchronous);
this.req.onerror = function () {
if (obj.options.proxy) {
obj.options.success = callback;
return graphite.create(proxy, obj.options);
}
callback("There was an error making the request\n");
};
this.req.onload = function () {
callback(null, obj.req.responseText, obj.req.status, obj.req);
};
this.req.send();
}
loader.xhr = xhr;
return xhr;
}(graphite, graphite.loader, graphite.loader.proxy));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment