【问题标题】:Twitter Bloodhound results from functionTwitter Bloodhound 函数的结果
【发布时间】:2014-05-29 08:47:50
【问题描述】:

我正在使用 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 过程。

【问题讨论】:

    标签: typeahead bloodhound


    【解决方案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()
    });
    

    【讨论】:

    • 我对这段代码感到困惑;不确定您在哪里引用 typeaheadResults 考虑到 source 设置为具有本地数组的适配器。
    • @pixelbobby 直接回答你的问题;第 7 行。 在问题中,他传递了一个函数作为调用 typeaheadResults 的源。使用 Bloodhound,您不会将过滤函数作为源传递,而是将数组作为数组传递,否则就没有理由使用 Bloodhound。因此,如果源是一个数组,您可以在 Bloodhound 构造函数选项中直接将其作为本地传递,如果它是不支持查询的网络资源,您可以在任何地方调用它并将其插入到异步回调中的 Bloodhound。就像我在第 7 行所做的一样。
    猜你喜欢
    • 1970-01-01
    • 2015-07-19
    • 1970-01-01
    • 2014-05-08
    • 1970-01-01
    • 2014-11-19
    • 2014-06-03
    • 1970-01-01
    • 2015-08-02
    相关资源
    最近更新 更多