I’m starting to see a pattern that I’m using for all my $.ajax calls, so I’d like to write a jQuery plugin called myAjax:
var myXHR = $.myAjax(myURL, mySettings)
and it will will always include the following:
myXHR.done(function(result) {
if (result.MSG) {
$('#msg').html(result.MSG).addClass('alert alert-info');
}
})
.fail(function(A,B,C) {
$('#msg').html(C).addClass('alert alert-info');
});
I also want to include the defaults that I use regularly:
var ajaxDefaults = {};
ajaxDefaults.type= 'POST'
ajaxDefaults.dataType='json';
$.ajaxSetup(ajaxDefaults);
Q: How do I write a plugin to do those two things:
- Include the defaults for type and dataType
- Include a default .done and .fail
To set a plugin in jQuery is quite simple as you follow your own documentation.
JQuery documentation
So for your case it will:
I hope this help.