jquery - 当提前输入 Bloodhound Remote 返回零结果时如何捕获事件

标签 jquery typeahead.js bootstrap-typeahead bloodhound

我正在使用 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/

相关文章:

javascript - Highcharts 阶梯线带有间隙和系列,如 [x,y]

typeahead.js - 加载 npm typeahead.js 时如何引用 typeahead 和 Bloodhound

twitter-bootstrap - bootstrap typeahead 防止默认选择

javascript - Bootstrap Typeahead 渲染问题

jquery - 如何在 typeahead 函数调用中使用 bootstrap 的 typeahead 获取文本框的属性

jquery - Bootstrap 3 与 Typeahead JSON 远程

javascript - 根据 Controller 值在 jquery 中展开列表项

javascript - 如何将 HTML Div 与线条连接起来?

jquery - jquery mobile 的导航栏中的选择未正确发生

javascript - 如何重置 typeahead.js 中使用的 Hogan.js 模板