如何在远程 typeahead.js 中聚焦已填写的文本字段时禁用 typeahead:active

how to disable typeahead:active when focusing a filled-in text field in a remote typeahead.js

我正在使用 typeahead.js,它很棒,但这是我的用例:我有一个文本字段,当页面加载时,它已经在服务器端填充了一个字符串,所以我不不希望在用户聚焦文本字段时显示建议菜单。

typeahead.js 似乎 总是 在聚焦文本字段时显示菜单。这是有道理的,因为 minLength 是 2 并且文本字段值类似于 "Tony"(或其他)。但我已经尝试使用 hint: false 并在 typeahead:active 事件的回调中调用 $('.typeahead').typeahead('close'),但似乎都没有阻止显示菜单。

这是我正在使用的初始化代码:

$('#locationInput').typeahead({
    hint: false,
    highlight: false,
    minLength: 2
},
{
    name: 'locations',
    display: 'name',
    source: typeaheadLocations, // a simple Bloodhound instance
    templates: {
        suggestion: function(locationObj) {
            return $('<div>' + locationObj.name + '</div>');
        }
    }
});

在我聚焦文本字段和实际显示菜单之间存在轻微延迟,因为远程 GET 必须是 运行。所以我猜我需要在它聚焦之后以某种方式阻止它。

typeahead:open 事件中关闭菜单似乎可以解决问题,但对我来说这似乎很老套。 :(

$('#locationInput').typeahead({
    hint: true,
    highlight: true,
    minLength: 2
},
{
    name: 'locations',
    display: 'name',
    source: typeaheadLocations,
    templates: {
        suggestion: function suggestion(locationObj) {
            return $('<div>' + locationObj.name + '</div>');
        }
    }

}).on('typeahead:open', function(e) {
    var $input = $(e.currentTarget);

    if (!$input.data('wasFocusedOnce')) {
        $input.data('wasFocusedOnce', true);
        $input.typeahead('close');
    }

}).on('typeahead:close', function(e) {
    $(e.currentTarget).removeData('wasFocusedOnce');
}).on('keydown', function(e) {
    $(e.currentTarget).data('wasFocusedOnce', true);
});

如果有人知道更好的方法,我会洗耳恭听!