Recently making the switch from Prototype to jQuery, I am struggling with some basic issues.
I use several AJAX-requests in my application; for 95% of these requests, I'd like to have some global events executed, such as showing or hiding a loading indicator. Apart from the global functionality, every request should also have some custom code executed after it has completed.
It seems like the local Ajax events override the global ones, once they are set. For example, see the following code:
// global events
$('#loader').bind('ajaxStart', function() {
// in this particular example, the ajaxStart() works fine,
// as it has not been overridden
Loader.enable();
}).bind('ajaxComplete', function() {
// this section will not execute. when the local complete()
// is removed, it will execute just fine
Loader.disable();
} );
// local ajax event
$.ajax({
type: $(element).attr('method'),
url: $(element).attr('action'),
data: $(element).serialize(),
global: 'true',
complete: function(data) {
if (params.target && $('#' + params.target)) {
$('#' + params.target).html(data.responseText);
}
Behaviour.apply();
});
In this particular case, the local complete() event seems to stop the global ajaxComplete() event from executing.
Is there any way to execute both the local as the global ajax events?