我正在使用带有猎犬建议引擎的twitter typeahead,一切正常。以下是我的代码段
// 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
};
});
}