Twitter Typeahead 遥控器未完成某些查询

Twitter Typeahead remote not completing some queries

我使用的是 0.11.1 版本的 Twitter Typeahead。现在我正试图让远程工作正常,但不知何故我认为我的行为很奇怪。

这是本地数组的工作代码:

var localArray = [{"value":"test0"},{"value":"test1"},{"value":"test2"},{"value":"test3"},{"value":"test4"}];
var myds = new Bloodhound({
    datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    local: localArray
});
$(function () {
    $("#my-input").typeahead({},
    {
        name: 'ds1',
        source: myds,
        display: 'value'
    });
});

现在我尝试设置远程数据。我所做的只是将本地更改为远程并提供 URL 并添加一个查询参数。它将传递与 localArray 完全相同的结构和数据(它目前忽略查询并始终传递 localArray 的内容只是为了测试这一点)。我在 Chrome 开发人员工具中检查了查询实际执行的内容和 returns localArray 的内容:

var myds = new Bloodhound({
    datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    remote: {
        url: '/autocomplete?query=%QUERY',
        wildcard: '%QUERY'
    }    
});
$(function () {
    $("#my-input").typeahead({},
    {
        name: 'ds1',
        source: myds,
        display: 'value'
    });
});

搜索 test 时不知何故这不起作用。当搜索 tes 时,我仍然得到结果,但是当我搜索 test 时,没有提供自动完成的结果。还有其他的词对此不起作用,并且它似乎遵循相同的模式,就像在找到部分词时找不到完整的词一样。知道是什么原因导致此行为或如何解决它吗?

我的猜测是远程数据源必须采用另一种格式,但我找不到它的外观。 typeahead 页面上的所有示例看起来都非常相似...

您遇到 bug 提前输入。在处理等于或小于 typeahead 中声明的限制(默认限制为 5)的结果集时,它会导致奇怪的行为。不幸的是,主要的 typeahead repo 不再维护,因此您将不得不使用 PR 来解决问题或自己编辑代码。幸运的是,修复只是向下移动了一行代码。

在已编译的包中或在src/typeahead/dataset.js中找到此代码块并将其更改为:

function async(suggestions) {
    suggestions = suggestions || [];
    if (!canceled && rendered < that.limit) {
        that.cancel = $.noop;
        that._append(query, suggestions.slice(0, that.limit - rendered));
        rendered += suggestions.length;
        that.async && that.trigger("asyncReceived", query);
    }
}

所有这一切都是移动 rendered += suggestions.length; 低于 that._append(query, suggestions.slice(0, that.limit - rendered));;

这在this PR中得到了例证。