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?

Answers


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; }
    });
});

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.


Need Your Help

Git pulling a branch from another repository?

git github git-branch

I have a local git repository which is a clone of a repository on github. Someone has forked the repository and made changes in a new branch on a new repository. I want to move this new branch into...

rspec test result from csv.read mocking file

ruby-on-rails file mocking rspec rspec2

I'm using ruby 1.9 and I'm trying to do BDD. My first test 'should read in the csv' works, but the second where I require a file object to be mocked doesn't.