首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >推特血犬源于功能

推特血犬源于功能
EN

Stack Overflow用户
提问于 2014-05-29 08:47:50
回答 1查看 889关注 0票数 1

我使用TypeAhead和血犬一起尝试实现函数返回的可缓存结果。

背后的想法如下:

  • TypeAhead叫猎犬
  • 血犬调用函数返回结果
  • 血犬缓存这些结果,并将其返回给TypeAhead
  • TypeAhead显示这些结果
  • 用户附加到输入,因此TypeAhead调用BloodHound来搜索缓存的结果,而不是对DB的另一个调用。
  • 用户清除文本框、TypeAhead和BloodHound重置。

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

代码语言:javascript
运行
复制
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检索结果.我在这方面没有多少经验,我试过以下几点:

代码语言:javascript
运行
复制
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做了很多工作,所以我不能简单地使用血犬的remote程序。

EN

回答 1

Stack Overflow用户

发布于 2014-07-30 16:24:56

我刚发现这个问题,我就这样解决了:

代码语言:javascript
运行
复制
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()
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23929285

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档