【发布时间】: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