Skip to content

Instantly share code, notes, and snippets.

@euforic
Created August 21, 2013 22:08
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save euforic/6300897 to your computer and use it in GitHub Desktop.
Save euforic/6300897 to your computer and use it in GitHub Desktop.
superagent XHR Titanium Shim
function XMLHttpRequest() {
// titanium xhr client
this._proxy = Ti.Network.createHTTPClient();
// mapping for compatible functions
this.getResponseHeader = this._proxy.getResponseHeader;
this.open = this._proxy.open;
this.send = this._proxy.send;
this.setRequestHeader = this._proxy.setRequestHeader;
this.abort = this._proxy.abort;
}
Object.defineProperties(XMLHttpRequest.prototype, {
'onreadystatechange' : {
set: function (val) {
return this._proxy.onreadystatechange = val
}
},
'readyState': {
get: function () {
return this._proxy.readyState;
}
},
'responseText': {
get: function () {
return this._proxy.responseText;
}
},
'responseXML': {
get: function () {
return this._proxy.responseXML;
}
},
'status': {
get: function () {
return this._proxy.status;
}
}
});
XMLHttpRequest.prototype.getAllResponseHeaders = function() {
return '';
};
this.XMLHttpRequest = XMLHttpRequest;
var location = this.location = {};
@shobhit-shrivastava
Copy link

could you please give me an example of how to use this in titanium project ?

@euforic
Copy link
Author

euforic commented Sep 20, 2013

@shobhit-shrivastava it is just a simple shim to make titaniums xhr library compatible with web xhr libraries. So if you just add it to your app.js you will be able to use libraries like superagent.js right out of the box.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment