I'm using this aswell, I call a lot of Ajax functions, and want a loader to show. If you use the code below, the original success function is saved and then removed from the settings because we want to hide our dialog on the success. Then within this success function, we call our saved success function that was added to our settings (which was removed).
// Backup original ajax, which is used at the bottom
var _ajax = $.ajax;
// Now overwrite it with our own function
$.ajax = function (properties) {
// Save the original success function
var successFunction = properties.success;
delete properties.success;
var _error = $.noop;
// Save error function
if (properties.error) {
_error = properties.error;
delete properties.error;
}
// TODO :: OPEN YOUR LOADER HERE
// Default settings
var settings = {
url: "ajax.html",
data: data,
async: true,
success: function (response) {
// TODO :: CLOSE YOUR LOADER HERE
successFunction.call(this, response);
},
error: function (xhr, textStatus, thrownError) {
// TODO :: CLOSE YOUR LOADER HERE
// Call error
_error.call(xhr, textStatus, thrownError);
}
};
// Extend with own properties
$.extend(settings, properties);
// Do the request
_ajax(settings);
};