如何禁用Bloodhound的ajax'global'标志(Typeahead.js)

时间:2017-01-19 16:23:33

标签: javascript typeahead.js bloodhound

我见过人们通过在构造函数中包含这样的Bloodhound对象来配置typeahead.js正在ajax中执行的AJAX请求:

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

但是,我一直在查看源代码,发现无法在最新版本(此时为v.0.11.1)上以这种方式配置ajax。 我需要关闭ajax标记global ,以便在向服务器发送ajax请求时不会触发我的加载gif,但我无法找到如何执行此操作

尝试这两种方式。您会注意到我按照指定网址的方式使用struts2

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

并且,在remote对象之外没有运气:

    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,
        }

如何关闭global ajaxBloodhound次来电的fork标记?

1 个答案:

答案 0 :(得分:0)

我的同事今天早上遇到了同样的情况,他通过使用带有if语句的$ .ajaxSend()来确定URL是否需要加载GIF动画,从而完成了一种解决方法。

希望这有助于:)