【问题标题】:typeahead/bloodhound with multiple remote datasources具有多个远程数据源的预输入/猎犬
【发布时间】:2015-01-05 00:58:03
【问题描述】:

我想使用 twitter 的 typeahead 来实现搜索自动提示。这个自动建议应该提供术语建议(terms)和产品建议(products)。这两个建议都可以远程检索。但是,typeahead 方法似乎只调用一个远程 URL。第二个永远不会被调用。

有人知道我做错了什么吗?这是我的代码:

$(document).ready(function () {
  var termEngine = new Bloodhound({
    name: 'searchTerms',
    remote: "/index.php?terms=true&g=%QUERY",
    datumTokenizer: Bloodhound.tokenizers.whitespace,
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    cache: false
  });
  var productEngine = new Bloodhound({
    name: 'products',
    remote: "/index.php?products=true&g=%QUERY",
    datumTokenizer: Bloodhound.tokenizers.whitespace,
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    cache: false
  });

  termEngine.initialize();
  productEngine.initialize();

  $('#searchField').typeahead(
      {
        name: 'searchTerms',
        source: termEngine.ttAdapter()
      }, {
        name: 'products',
        source: productEngine.ttAdapter()
  });
}

我还添加了一个具有相同问题的 jsfiddle 链接(尽管远程数据源不同):http://jsfiddle.net/r4cnares/1/

【问题讨论】:

    标签: javascript typeahead.js


    【解决方案1】:

    问题解决了:我必须在typeahead方法中添加一个空选项对象:

    $('#searchField').typeahead({},
      {
        name: 'searchTerms',
        source: termEngine.ttAdapter()
      }, {
        name: 'products',
        source: productEngine.ttAdapter()
    });
    

    这样可以确保两个请求都被执行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多