Disable BlockUI for certain ajax calls
Asked Answered
G

2

5

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?

Gene answered 11/3, 2011 at 9:10 Comment(0)
L
8

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; }
    });
});
Litter answered 11/3, 2011 at 9:23 Comment(0)
I
24

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.

Interpose answered 29/4, 2011 at 3:19 Comment(1)
Awesome! Learned this today... :)Vitta
L
8

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; }
    });
});
Litter answered 11/3, 2011 at 9:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.