1

我正在使用 TypeAhead 和 Bloodhound 来尝试实现从函数返回的可缓存结果。

背后的思路如下:

  • TypeAhead 调用 Bloodhound
  • Bloodhound 调用函数返回结果
  • Bloodhound 缓存这些结果,并将它们返回给 TypeAhead
  • TypeAhead 显示这些结果
  • 用户追加到输入,因此 TypeAhead 调用 BloodHound 来搜索缓存的结果,而不是再次调用数据库。
  • 用户清除文本框、TypeAhead 和 BloodHound 重置

目前,每次用户更改输入时,我都会直接从结果函数调用 TypeAhead:

jQuery(element).typeahead({
    hint: true,
    highlight: true, // This is to bold words that match the query
    minLength: 3
}, {
    name: "result",
    displayKey: "value",
    source: function (query, callback) {

        typeaheadResults(query, callback);

    }
});

但是,我希望 BloodHound 检索结果......我没有太多经验,并尝试了以下方法:

var bhResults = new Bloodhound({
    datumTokenizer: function (d) { return Bloodhound.tokenizers.whitespace(d.num); },
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    local: // What do I do here? function typeaheadResults needs the 'query'
});

typeaheadResults 做了很多工作,所以我不能简单地使用 BloodHound 的remote程序。

4

1 回答 1

1

我也遇到了这个问题,我是这样解决的:

var bhResults = new Bloodhound({
    datumTokenizer: function (d) { return Bloodhound.tokenizers.whitespace(d.num); },
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    local: []
});

typeaheadResults(/* you don't need a query with bloodhound */, function(result){ bhResults.add(result); });

bhResults.initialize();

jQuery(element).typeahead({
    hint: true,
    highlight: true, // This is to bold words that match the query
    minLength: 3
}, {
    name: "result",
    displayKey: "value",
    source: bhResults.ttAdapter()
});
于 2014-07-30T16:24:56.863 回答