我知道只有当预取结果不超过限制集时才应使用远程。我将限制设置为1,预取有多个条目。它仍然使用远程来获取数据,最终只显示1个结果。
initialise_quick_search: function () {
var building_data = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('building_name'),
queryTokenizer: Bloodhound.tokenizers.nonword,
prefetch: {
url: '/quick_search/quick_search_results?search_buildings=1',
//cache: false
},
remote: {
url: '/quick_search/quick_search_results?search_buildings=1&query=%QUERY',
wildcard: '%QUERY',
cache: false
}
});
building_data.initialize();
var $quick_search = $('#quick_search');
$quick_search.typeahead({
highlight: true,
hint: false,
minLength: 2,
},
{
name: 'buildings',
display: 'building_name',
source: building_data,
limit: 1,
templates: {
header: 'Buildings',
suggestion: function (data) {
return '<p><a target="_blank" href=' + data.href + '>' + data.text + '</a></p>'
}
}
}
);
$quick_search.bind('typeahead:close', function (ev, suggestion) {
$quick_search.val('');
$quick_search.typeahead('close');
});
$quick_search.bind('typeahead:select', function (ev, suggestion) {
window.open(suggestion.href, '_blank');
});
}
如果有人希望在预取中至少有一个条目时不触发远程,请在typeahead.bundle.js文件中更改下面代码段中的足够值
var oParser = function() {
"use strict";
return function parse(o) {
var defaults, sorter;
defaults = {
initialize: true,
identify: _.stringify,
datumTokenizer: null,
queryTokenizer: null,
matchAnyQueryToken: false,
sufficient: 5,
indexRemote: false,
sorter: null,
local: [],
prefetch: null,
remote: null