【问题标题】:Typeahead Wait issues multiple requests instead of just the last oneTypeahead Wait 发出多个请求,而不仅仅是最后一个
【发布时间】:2013-09-24 22:20:58
【问题描述】:

我正在使用bootstrap's typeahead 从数据库中获取“姓氏”。我已阅读文档,我认为一切正常。但是,当我开始搜索时,我意识到“搜索”太慢了,我认为这是因为“预输入”会在我每次按键时发送请求。

如何在填充文本框后停止发送许多请求或尝试发送整个字符串?

也许这些信息会有所帮助

这是风景:

这是开发者工具的视图:

这是 JS:

我使用回调来发送请求:

var familias = {};
var familiaLabels = [];  

//Callback for searching the string
var buscarFamilia = _.debounce(function( query, process ){

    $.getJSON( '{/literal}{$smarty.const.FAMILIARES_FE_ROOT}{literal}/ws/familia-ajax/', { q: query }, function ( data ) {

        //Clean containers
        familias = {};
        familiaLabels = [];

        //Using some underscore.js for getting data from each item 
        _.each( data, function( item, ix, list ){

            //Fill with the name of each item
            familiaLabels.push( item.nombre );

            //Fill data for the template
            familias[ item.nombre ] = {

              id:item.id,
              nombre:item.nombre,
              padre:item.padre,
              madre:item.madre

            };
        });

        //Return the array
        process( familiaLabels );
    },800);

}); 

这是“预先输入”的配置:

$('#alu_familia').typeahead({
    source: function ( query, process ) {

        buscarFamilia ( query, process )
    }
    , updater: function (itemSelected) {

        //This is for getting the id
        $( "#alu_fami_id" ).val( familias[ itemSelected].id );


        return itemSelected;
    }
    ,
    minLength:2,
    matcher: function () { return true; }
    ,highlighter: function(item){
        var p = familias[ item ];
        var itm = ''
                 + "<div class='typeahead_wrapper'>"
                 + "<div class='typeahead_labels'>"
                 + "<div class='typeahead_primary'>" + p.nombre + "</div>"
                 + "<div class='typeahead_secondary'><b>Padre: </b>" + p.padre +"</div>"
                 + "<div class='typeahead_secondary'><b>Madre: </b>"+p.madre+ "</div>"
                 + "</div>"
                 + "</div>";
        return itm;
    }
  });

提前致谢。

【问题讨论】:

    标签: javascript jquery twitter-bootstrap bootstrap-typeahead typeahead


    【解决方案1】:

    您可以通过将任何以前检索到的值存储在缓存对象中来避免对同一查询发出两个 Ajax 请求,如下所示:

    变种家庭 = {}; var familiaLabels = []; var familiasCache = {}; //搜索字符串的回调 var buscarFamilia = _.debounce(函数(查询,处理){ if (typeof familiasCache[query] !== "undefined") { 返回过程(familiasCache[查询]); } $.getJSON('{/literal}{$smarty.const.FAMILIARES_FE_ROOT}{literal}/ws/familia-ajax/', { q: query }, function (data) { //清理容器 家庭 = {}; 家庭标签 = []; //使用一些 underscore.js 从每个项目中获取数据 _.each(数据,函数(项目,ix,列表){ //填写每个项目的名称 familiaLabels.push(item.nombre); //为模板填充数据 家族[item.nombre] = { id:item.id, 名称:item.nombre, 教士:item.padre, madre:item.madre }; }); // 将结果存入缓存 familiasCache[查询] = familiaLabels; //返回数组 过程(家庭标签); },800);

    【讨论】:

    • 感谢您的回复,但是正如您所说,这个修改只是存储了之前检索到的值。这很好,因为它可以防止在重复单词时发送任何请求。但是,我想知道的是,当用户停止输入时,如何仅向服务器发送一个请求。
    • 您的代码正在使用_.debounce(),因此该函数最多每 800 毫秒调用一次。因为这里的事件是keryup,所以您无法判断用户何时停止输入,除非您愿意进行一些复杂的时间计算,这肯定会花费您一些响应能力。所以你唯一的选择就是微调去抖时间。
    猜你喜欢
    • 2022-06-15
    • 2013-06-11
    • 1970-01-01
    • 1970-01-01
    • 2016-06-10
    • 2022-12-10
    • 2012-10-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多