我正在使用 Twitter typeahead 和 Bloodhound 建议引擎,一切正常。下面是我的代码片段
// instantiate the bloodhound suggestion engine
var searchData = new Bloodhound({
datumTokenizer: function(d) {
return Bloodhound.tokenizers.whitespace(d.value);
},
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: '<?php echo 'http://localhost/project1/perform/find?q=%QUERY'; ?>',
filter: function (data) {
return $.map(data.results, function (record) {
return {
title: record.title,
pageURL: record.pageURL
};
});
}
}
});
// initialize the bloodhound suggestion engine
searchData.initialize();
searchData.clearRemoteCache();
// instantiate the typeahead UI
$('#find').typeahead({
hint:false,
highlight: true,
minLength: 3
}, {
name:'search-data',
displayKey: 'title',
source: searchData.ttAdapter(),
templates: {
empty:[
'<strong>No Results Found.</strong>'
],
suggestion: Handlebars.compile('<p>{{title}}</p>')
}
}).on('typeahead:selected', function (e, suggestion) {
setTimeout(function(){document.location = suggestion.pageURL;}, 500);
}).on('typeahead:closed', function (e){
$loadingImg.hide();
});
我想做一些操作,比如显示发布按钮等,当远程服务器返回零结果时,如何捕获这个事件?
最佳答案
不知道下面的方法是否正确(如有错误请指正)
filter: function (data) {
if(data.results.length){
console.log('results found'); //do something
}else{
console.log('results not found'); //do something
}
return $.map(data.results, function (record) {
return {
title: record.title,
pageURL: record.pageURL
};
});
}
关于jquery - 当提前输入 Bloodhound Remote 返回零结果时如何捕获事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25302888/