【问题标题】:jQuery UI autocomplete caching using $.map function使用 $.map 函数的 jQuery UI 自动完成缓存
【发布时间】:2011-05-23 03:43:32
【问题描述】:

我正在尝试使用 jQuery UI 自动完成来实现缓存。 我正在使用 jQuery 1.4.4 和 UI 1.8.6

这是我开始工作的基本代码:

$('#searchbox').autocomplete({
    source: function(request, response) {
            if (xhr === lastXhr) {
                response( $.map(data, function(item) {
                    return {
                        label: item.NAME + (item.PRFNM ? ' (' + item.PRFNM + ')' : '') + ', ' + item.JOBTITLE,
                        value: item.NAME
                    };
                }));
            } 
        });
    }
});

这是我通过查看示例尝试使缓存起作用的尝试:

var cache = {},
    lastXhr;
$('#searchbox').autocomplete({
    source: function(request, response) {
        var term = request.term;
        if (term in cache) {
            response($.map(cache[term], function(item) {
                return {
                    label: item.NAME + (item.PRFNM ? ' (' + item.PRFNM + ')' : '') + ', ' + item.JOBTITLE,
                    value: item.NAME
                };
            }));
        }
        lastXhr = $.getJSON( "getdata.php", request, function(data, status, xhr) {
            cache[term] = $.map(data, function(item) {
                return {
                    label: item.NAME + (item.PRFNM ? ' (' + item.PRFNM + ')' : '') + ', ' + item.JOBTITLE,
                    value: item.NAME
                };
            }); 
            if (xhr === lastXhr) {
                response( $.map(data, function(item) {
                    return {
                        label: item.NAME + (item.PRFNM ? ' (' + item.PRFNM + ')' : '') + ', ' + item.JOBTITLE,
                        value: item.NAME
                    };
                }));
            } 
        });
    }
});

那里有接受者吗?

【问题讨论】:

  • 它没有像应有的那样缓存。

标签: javascript jquery-ui caching autocomplete jquery-autocomplete


【解决方案1】:

当我试图将 $.map 函数放入其中时,问题在于我的缓存[术语],因为它不需要。

cache[term] = $.map(data, function(item) {
                    return {
                        label: item.NAME + (item.PRFNM ? ' (' + item.PRFNM + ')' : '') + ', ' + item.JOBTITLE,
                        value: item.NAME
                    };
                });

所以这是我为那些仍然有问题的人准备的最终脚本: 为了避免混淆,我还保留了所有选项。

var cache = {},
 lastXhr;

$('#searchbox').autocomplete({
    source: function(term, response) {
        var term = term;
        if (term in cache) {
            response($.map(cache[term], function(item) {
                return {
                    /*Format autocomplete to display name and job title, e.g., Smith, John, Web Developer*/
                    label: item.NAME + (item.PRFNM ? ' (' + item.PRFNM + ')' : '') + ', ' + item.JOBTITLE,
                    /*Replace the searched value with the value selected.*/
                    value: item.NAME
                };
            }))
            /*I happened to leave this out, which I think what one of the main cause my caching did not work.*/
            return;
        }
        lastXhr = $.getJSON( "getdata.php", request, function(data, status, xhr) {
            cache[term] = data;
            if (xhr === lastXhr) {
                response($.map(data, function(item) {
                    return {
                        label: item.NAME + (item.PRFNM ? ' (' + item.PRFNM + ')' : '') + ', ' + item.JOBTITLE,
                        value: item.NAME
                    };
                }));
            } 
        });
    }
});

【讨论】:

    【解决方案2】:

    这是我使用 cache 的 jQuery UI 自动完成的工作示例。希望对您有所帮助:

        var cache = {};
        $("#textbox").autocomplete({
          source: function(request, response) {
           if (request.term in cache) {
            response($.map(cache[request.term].d, function(item) {
             return { value: item.value, id: item.id }
            }))
            return;
           }
           $.ajax({
            url: "/Services/AutoCompleteService.asmx/GetEmployees",  /* I use a web service */
            data: "{ 'term': '" + request.term + "' }",
            dataType: "json",
            type: "POST",
            contentType: "application/json; charset=utf-8",
            dataFilter: function(data) { return data; },
            success: function(data) {
             cache[request.term] = data;
             response($.map(data.d, function(item) {
              return {
               value: item.value,
               id: item.id
              }
             }))
            },
            error: HandleAjaxError  // custom method
           });
          },
          minLength: 3,
          select: function(event, ui) {
           if (ui.item) {
            formatAutoComplete(ui.item);   // custom method
           }
          }
         });
    

    如果您现在还没有这样做,请联系 Firebug。它是 Web 开发的宝贵工具。你可以在这个 JavaScript 上设置一个断点,看看会发生什么。

    【讨论】:

    • 谢谢拉斐尔!我看到我做错了。没有萤火虫我活不下去!
    猜你喜欢
    • 1970-01-01
    • 2011-04-07
    • 2011-05-22
    • 1970-01-01
    • 1970-01-01
    • 2018-01-18
    • 2011-03-10
    • 2012-07-11
    • 1970-01-01
    相关资源
    最近更新 更多