Skip to content

Instantly share code, notes, and snippets.

@mmohiudd
Created March 19, 2012 17:33
Show Gist options
  • Save mmohiudd/2120494 to your computer and use it in GitHub Desktop.
Save mmohiudd/2120494 to your computer and use it in GitHub Desktop.
API prototype
/**
* API prototype class. Uses strict ECMA convention.
* This class requires jQuery.
*
* @category Prototype
* @author Muntasir Mohiuddin
*/
var API;
if (API == undefined) {
API = function (endPoint) {
this.init(endPoint);
};
}
API.prototype.endPoint = null;
API.prototype.init = function(endPoint){
this.endPoint = endPoint;
};
API.prototype.ajax = function(uri, type, data, success_callback, error_callback){
console.log(this.endPoint+uri+"?"+data);
console.log(data);
$.ajax({
url: this.endPoint+uri,
type: type,
data: data,
crossDomain: true,
success: function(response){
try{
success_callback(response);
} catch(e){}
},
error: function(jqXHR, textStatus, errorThrown){
try{
error_callback(jqXHR, textStatus, errorThrown);
} catch(e){}
}
});
};
API.prototype.post = function(uri, data, success_callback, error_callback) {
this.ajax(uri, "POST", data, success_callback, error_callback);
};
API.prototype.get = function(uri, data, success_callback, error_callback){
this.ajax(uri, "GET", data, success_callback, error_callback);
};
API.prototype.create = function(data, success_callback, error_callback) {
this.post("/create", data, success_callback, error_callback);
};
API.prototype.read = function(data, success_callback, error_callback) {
this.get("/read", data, success_callback, error_callback);
};
API.prototype.update = function(data, success_callback, error_callback) {
this.post("/update", data, success_callback, error_callback);
};
API.prototype.delete = function(data, success_callback, error_callback) {
this.post("/delete", data, success_callback, error_callback);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment