Skip to content

Instantly share code, notes, and snippets.

@yamoo9
Last active August 3, 2018 03:26
Show Gist options
  • Save yamoo9/206f191d6a4c0d7342c9c73dd053b9fd to your computer and use it in GitHub Desktop.
Save yamoo9/206f191d6a4c0d7342c9c73dd053b9fd to your computer and use it in GitHub Desktop.
jQuery Ajax 단축 메서드 - PUT / PATCH / DELETE 확장
/*! jquery.ajax.extends.js @ 2018, yamoo9.net */
;(function(global, $) {
'use strict';
if (!$) { throw new Error('jQuery 라이브러리를 호출해야 사용 가능합니다.'); }
if ( !$.put ) {
$.put = function(api_with_id, put_data, success, fail) {
$.ajax({
method: 'PUT',
url: api_with_id,
data: put_data,
})
.then(
$.type(success) === 'function' ? success : function(){},
$.type(fail) === 'function' ? fail : function(){}
);
};
}
if ( !$.patch ) {
$.patch = function(api_with_id, patch_data, success, fail) {
$.ajax({
method: 'PATCH',
url: api_with_id,
data: patch_data,
})
.then(
$.type(success) === 'function' ? success : function(){},
$.type(fail) === 'function' ? fail : function(){}
);
};
}
if ( !$.delete ) {
$.delete = function(api_with_id, success, fail) {
$.ajax({
method: 'DELETE',
url: api_with_id,
})
.then(
$.type(success) === 'function' ? success : function(){},
$.type(fail) === 'function' ? fail : function(){}
);
};
}
if ( !$.jsonp ) {
$.jsonp = function(api_address, success, fail) {
$.ajax({
url: api_address,
dataType: 'jsonp',
})
.then(
$.type(success) === 'function' ? success : function(){},
$.type(fail) === 'function' ? fail : function(){}
);
};
}
})(window, window.jQuery);
!function(n,e){"use strict";if(!e)throw new Error("jQuery 라이브러리를 호출해야 사용 가능합니다.");e.put||(e.put=function(n,t,o,u){e.ajax({method:"PUT",url:n,data:t}).then("function"===e.type(o)?o:function(){},"function"===e.type(u)?u:function(){})}),e.patch||(e.patch=function(n,t,o,u){e.ajax({method:"PATCH",url:n,data:t}).then("function"===e.type(o)?o:function(){},"function"===e.type(u)?u:function(){})}),e.delete||(e.delete=function(n,t,o){e.ajax({method:"DELETE",url:n}).then("function"===e.type(t)?t:function(){},"function"===e.type(o)?o:function(){})}),e.jsonp||(e.jsonp=function(n,t,o){e.ajax({url:n,dataType:"jsonp"}).then("function"===e.type(t)?t:function(){},"function"===e.type(o)?o:function(){})})}(window,window.jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment