【发布时间】:2013-05-09 18:46:32
【问题描述】:
我在我的应用程序中使用 twitter 引导库,这是一次很棒的体验。现在我正在为自动完成实现 bootstrap typeahad。 在这里,我在缓存结果以避免对服务器的许多请求时遇到问题。在网上进行研究时,我发现了这个库https://github.com/twitter/typeahead.js,也称为 twitter typeahead。这似乎与 boottrap http://twitter.github.io/bootstrap/javascript.html#typeahead 上的非常不同。我错了吗?
第一个选项似乎最适合预期,但我有以下问题:
-
如何使用预取?这必须是预先存在的 json 文件,或者可能是页面加载时对服务器的第一个请求?
是否有任何使用预取和远程的工作示例?
仅供参考:这是我的实际代码,带有 twitter boottrap typeahead(不是 twitter typeahead)
$searchbox.typeahead(
{
minLength: 2,
source: function (query, process) {
$SearchID.val('');
persons = [];
map = {};
$.getJSON("App_Services/MyService.svc/GetData", { max: 100, criteria: query }, function (data) {
$.each(data, function (i, person) {
map[person.name] = person;
persons.push(person.name);
});
process(persons);
});
},
updater: function (item) {
selected = map[item].id;
//alert(selected)
return item;
},
matcher: function (item) {
if (item.toLowerCase().indexOf(this.query.trim().toLowerCase()) != -1) {
return true;
}
},
sorter: function (items) {
return items.sort();
},
highlighter: function (item) {
var regex = new RegExp('(' + this.query + ')', 'gi');
return '<div>' + item.replace(regex, "<strong>$1</strong>") + '</div>';
}
})
只是为了澄清。我要做的是创建结果缓存,如果在缓存中找不到匹配项,则仅使用服务器。
【问题讨论】:
标签: jquery json twitter-bootstrap