-
Notifications
You must be signed in to change notification settings - Fork 1
/
jquery.restly.min.js
7 lines (7 loc) · 3.04 KB
/
jquery.restly.min.js
1
2
3
4
5
6
7
(function($){$.fn.restly=function(options){var opts=$.extend({},$.fn.restly.defaults,options);return this.each(function(){var element=$(this);element.click(function(event){event.preventDefault();var confirmMessage=element.data("confirm");if(bootbox&&confirmMessage)bootbox.confirm(confirmMessage,function(confirmed){if(confirmed)$.fn.restly.processClick(element,opts)});else $.fn.restly.processClick(element,opts)});return this})};$.fn.restly.processClick=function(element,options){var opts=$.extend({},
options,element.data());$.fn.restly.send(opts)};$.fn.restly.send=function(options){var opts=$.extend({},$.fn.restly.defaults,options);if(!opts["method"])throw new Error("No method given");if(!opts["endpoint"])throw new Error("No endpoint given");if(!opts["resource"])throw new Error("No resource given");opts["method"]=opts["method"].toUpperCase();if((opts["method"]=="DELETE"||(opts["method"]=="PUT"||opts["method"]=="PATCH"))&&!opts["id"])throw new Error("No resource id given");if(typeof opts["redirect"]==
"string")opts["success"]=function(data,textStatus,jqXHR){window.location.href=opts["redirect"]};else if(typeof opts["success"]=="string"){var successCallback=window[opts["success"]];opts["success"]=function(data,textStatus,jqXHR){successCallback(data,textStatus,jqXHR)}}if(typeof opts["error"]=="string"){var errorCallback=window[opts["error"]];opts["error"]=function(jqXHR,textStatus,errorThrown){errorCallback(jqXHR,textStatus,errorThrown)}}if(opts["method"]=="POST"||(opts["method"]=="PUT"||opts["method"]==
"PATCH"))opts["data"]=JSON.stringify($.fn.restly.prepareAdditionalFieldData(opts["fields"]));opts["url"]=$.fn.restly.buildApiUrl(opts);$.ajax(opts)};$.fn.restly.get=function(options){options["method"]="GET";$.fn.restly.send(options)};$.fn.restly["delete"]=function(options){options["method"]="DELETE";$.fn.restly.send(options)};$.fn.restly.post=function(options){options["method"]="POST";$.fn.restly.send(options)};$.fn.restly.put=function(options){options["method"]="PUT";$.fn.restly.send(options)};$.fn.restly.patch=
function(options){options["method"]="PATCH";$.fn.restly.send(options)};$.fn.restly.prepareAdditionalFieldData=function(fields){var data={};if(fields)$.each(fields.split(","),function(index,fieldName){data[fieldName]=$("[name="+fieldName+"]").val()});return data};$.fn.restly.buildApiUrl=function(options){var requestUrl=options["endpoint"].concat("/").concat(options["resource"]);if($.isNumeric(options["id"]))requestUrl=requestUrl.concat("/").concat(options["id"]);return requestUrl};$.fn.restly.processErrorResponse=
function(jqXHR,textStatus,errorThrown){if(toastr)if(jqXHR.status=="500")toastr.error(jqXHR.statusText);else if(jqXHR.responseText){var response=$.parseJSON(jqXHR.responseText);toastr.error(response.userMessage)}else toastr.error("Invalid response.")};$.fn.restly.defaults={endpoint:null,resource:null,id:null,contentType:"application/json; charset=utf-8",method:"GET",dataType:"json",fields:null,redirect:null,success:jQuery.noop,error:$.fn.restly.processErrorResponse};if(typeof window["restly"]!="function")window["restly"]=
$.fn.restly})(jQuery);