How to show ajax indicator for nested ajax call?
I have made an ajax indicator and registered it to the global ajax event:
- $("<div></div>")
- .attr('id', 'ajaxLoader')
- .text('Loading...')
- .appendTo("body")
- .bgIframe()
- .bind("ajaxSend", function(ev){
- console.log("ajaxSend");
- $(this).show();
- })
- .bind("ajaxComplete", function(ev){
- console.log('ajaxComplete');
- $(this).hide();
- });
Then I have an ajax request like (using validation and ajaxForm plugin):
- $("#form").validate({
- submitHandler: function(){
- $("#form").ajaxSubmit({
- success: function(response, data){
- console.log("before load");
- $("#someUi").load("/someUrl.rpc");
- }
- });
- }
- });
It turns out that the ajax indicator is hidden when calling .load(), the console.log shows:
- ajaxSend
- [XHR.....]
- before load
- ajaxSend
- ajaxComplete
- [XHR.....]
- ajaxComplete
So the indicator is hidden by the first ajax complete call because of the nested nature of the code. So I have to implement something so that ajaxComplete will check if there is any ajax activity in the way and do not hide the indicator. Any suggestion to this?