Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable BlockUI for certain ajax calls

I am using the brilliant BlockUI, and have set it up using the default

$(document).ajaxStart($.blockUI).ajaxStop($.unblockUI);

Which is great - except when I add a autocomplete element on the page, and then the blockUI kicks in as soon as the user starts typing. Rather than explicitly set what ajax calls to launch the block UI can anyone think of a way to disable blockUI for certain ajax functions?

like image 731
Xrender Avatar asked Mar 11 '11 09:03

Xrender


2 Answers

In jQuery an ajax call accepts different settings A set of key/value pairs that configure the Ajax request. All settings are optional.

Now here we have the following:

settings name: global,Type: Boolean, Default: true

Description: Whether to trigger global Ajax event handlers for this request. The default is true. Set to false to prevent the global handlers like ajaxStart or ajaxStop from being triggered. This can be used to control various Ajax Events.

Example:

$.ajax({
        global: false,
        type: "POST",
        url: 'ajax/test.html',
        success: function(data) {
          $('.result').html(data);
          alert('Load was performed.');
        }
});

Above example will stop execution of any ajax start or ajax stop events like blockUI.

like image 122
Indranil Avatar answered Oct 14 '22 03:10

Indranil


You could do something like this:

var dontBlock = false;

$(document).ajaxStart(function(){
    if(!dontBlock)
        $.blockUI();
}).ajaxStop($.unblockUI);

// And in the ajax methods (to be excluded), set dontBlock accordingly

$('#autocomplete').keydown(function(){
    dontBlock = true;
    $.ajax({
        // url, data etc.
        success: function(){ dontBlock = false; }
    });
});
like image 37
Simeon Avatar answered Oct 14 '22 04:10

Simeon