Last active
December 19, 2015 01:38
-
-
Save freiguy1/5877022 to your computer and use it in GitHub Desktop.
Ajax utility file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
********************UTILITY********************* | |
*/ | |
var Ajaxable = function(url){ | |
this.create = function(newObject, onDone, onFail) { | |
$.ajax({ | |
type: "POST", | |
url: "/api/" + url, | |
data: JSON.stringify(newObject), | |
contentType: "application/json" | |
}).done(onDone).fail(onFail) | |
}; | |
this.update = function(id, updatedObject, onDone, onFail) { | |
$.ajax({ | |
type: "PUT", | |
url: "/api/" + url + "/" + id, | |
data: JSON.stringify(updatedObject), | |
contentType: "application/json" | |
}).done(onDone).fail(onFail) | |
}; | |
this.delete = function(id, onDone, onFail) { | |
$.ajax({ | |
type: "DELETE", | |
url: "/api/" + url + "/" + id | |
}).done(onDone).fail(onFail) | |
}; | |
this.getById = function(id, onDone, onFail) { | |
$.ajax({ | |
type: "GET", | |
url: "/api/" + url + "/" + id | |
}).done(onDone).fail(onFail) | |
}; | |
this.getAll = function(onDone, onFail) { | |
$.ajax({ | |
type: "GET", | |
url: "/api/" + url | |
}).done(onDone).fail(onFail) | |
}; | |
} | |
/* | |
********************POI********************* | |
ex: | |
poiAjax.create({ | |
name: "new poi", | |
detailSection: "<p>NOthing to see here, just a <h4>detail section</h4></p>", | |
latitude:44.749757, | |
longitude:-90.204021, | |
parentPoiListId:1 | |
}, | |
function(data){alert("Completed! Data = " + JSON.stringify(data))}, | |
function(){alert("Failed!")} | |
) | |
or | |
poiAjax.getByPoiListId(3, function(data){alert("Completed! Data = \n" + JSON.stringify(data))}) | |
*/ | |
var poiAjax = new Ajaxable("pois"); | |
poiAjax.updatePoiListForPoi = function(id, poiListId, success, failure){ | |
$.ajax({ | |
type: "PUT", | |
url: "/api/poiLists/" + poiListId + "/pois/" + id | |
}).done(success).fail(failure) | |
}; | |
poiAjax.getByPoiListId = function(poiListId, success, failure){ | |
$.ajax({ | |
type: "GET", | |
url: "/api/poiLists/" + poiListId + "/pois" | |
}).done(success).fail(failure) | |
}; | |
/* | |
********************POI LIST********************* | |
ex: | |
poiListAjax.create({name:"example poiList", parentCategoryId:3}) | |
*/ | |
var poiListAjax = new Ajaxable("poiLists") | |
/* | |
********************CATEGORY********************* | |
*/ | |
var categoryAjax = new Ajaxable("categories") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment