2

I've seen people configure the AJAX request Bloodhound is performing in typeahead.js by including an ajax object like this on their constructor:

var locsData = new Bloodhound({
  datumTokenizer: Bloodhound.tokenizers.obj.whitespace("name"),
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  remote: {
    url: "/locations/search.json?q=%QUERY",
    ajax: {
      beforeSend: function(xhr, settings) {
        // maybe the 'if' is not necessary, but just to be sure...
        if ($(document.activeElement).typeahead != null) {
          $(document.activeElement).addClass('loading-text');
        }
      },
      complete: function(xhr, status) {
        if ($(document.activeElement).typeahead != null) {
          $(document.activeElement).removeClass('loading-text');
        }
      }
    }
  },
  limit: 100
});

However, I've been taking a look at the sources and found no way to configure ajax that way on latest version (v.0.11.1 at this time). I need to turn off ajax flag global so it doesn't trigger my loading gifs when sending ajax requests to the server but I can't find out how to do it.

Tried this both ways. You'll notice that I'm using struts2 by the way I specified the URLs:

var engine = new Bloodhound({
            remote: {
                url: '<s:url value="%{#typeaheadHwsnUrl}"/>'+'%QUERY%',
                wildcard: '%QUERY%',
                ajax: {
                    global: false,
                }
            },
            datumTokenizer: Bloodhound.tokenizers.whitespace('q'),
            queryTokenizer: Bloodhound.tokenizers.whitespace
        });

and also, with no luck tried outside of the remote object:

    var engine = new Bloodhound({
        remote: {
            url: '<s:url value="%{#typeaheadHwsnUrl}"/>'+'%QUERY%',
            wildcard: '%QUERY%',
        },
        datumTokenizer: Bloodhound.tokenizers.whitespace('q'),
        queryTokenizer: Bloodhound.tokenizers.whitespace,
        ajax: {
            global: false,
        }

How can I turn off this global flag for ajax calls made by Bloodhound?

JorgeGRC
  • 1,032
  • 2
  • 18
  • 37

1 Answers1

0

My colleague ran into same scenario this morning and he has done a workaround by using $.ajaxSend() with an if-statement to determine whether the URL needs a loading GIF animation or not.

Hope this helped :)

Manaf
  • 41
  • 7
  • I ended up doing something similar, but I'd leave this open for now and see if somehow somebody knows the "right" way to address this issue, thanks for your answer though! – JorgeGRC Apr 17 '17 at 08:05