First, you should be careful about extending native object prototypes.
Second, if you're using jQuery, what you're wanting to do is pretty easily achieved using $.ajaxStart()
and $.ajaxComplete()
to set up a start and end action for all ajax calls. You can read about those here: https://api.jquery.com/ajaxStart/, and here: https://api.jquery.com/ajaxComplete/.
A basic implementation:
$( document )
.ajaxStart(function() {
$( e.target )closest('.ajaxContainer').addClass('pre-loader');
})
.ajaxComplete(function() {
$( e.target )closest('.ajaxContainer').removeClass('pre-loader');
});
That would work for any clicks in your document that trigger an ajax request.
Zepto fires a handful of events during the lifecycle of and ajax request, so you can do the same thing as in jQuery using Zepto's ajaxBeforeSend
and ajaxComplete
:
$(document)
.on('ajaxBeforeSend', function(e, xhr, options) {
// show your loader
})
.on('ajaxComplete', function(e, xhr, options) {
// hide your loader
});
According to their documentation, those events are fired on the document object so you can listen for them globally and they'll run for every ajax request you make. ajaxBeforeSend
gives you access to your request object, the event that fired the ajax request and the options passed to the request. So you can do a lot with that. You can read more here: http://zeptojs.com/#$.ajax