![](/img/trans.png)
[英]typeahead.js hint and highlight param not working with prefetch & remote
[英]Typeahead.js remote works, but prefetch doesn't
我在Web应用程序中使用typeahead.js 0.10.5。 出于某些奇怪的原因,预取操作被破坏时,通过远程工作实时获取建议。 这里发生了一些不明显和奇怪的事情。 根据开发控制台和Chrome的网络监控器,它甚至没有在页面加载时进行查询。 当我开始输入时,它确实会进行查询。
这真的让我感到难过-我在这里做错了什么?
// Instantiate the Bloodhound suggestion engine
var tags = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: '/tags/tags/search.json?q=%QUERY',
filter: function (taglist) {
// Map the remote source JSON array to a JavaScript object array
return $.map(taglist, function (tag) {
console.log(tag);
return {
value: tag.tag
};
});
}
},
prefetch: {
url: '/tags/tags/search.json?q=',
filter: function (taglist) {
// Map the remote source JSON array to a JavaScript object array
return $.map(taglist, function (tag) {
console.log(tag);
return {
value: tag.tag
};
});
},
}
});
// Initialize the Bloodhound suggestion engine
tags.initialize();
// Instantiate the Typeahead UI
$('#search-tags').typeahead(null, {
displayKey: 'value',
source: tags.ttAdapter(),
hint: true,
highlight: true
});
尝试从浏览器的localStorage中删除条目,然后重新开始。
默认情况下,Bloodhound对象将预取的数据缓存在浏览器的本地存储中,为它分配1天的TTL(生存时间),并且直到TTL过期才重新验证它。 初始化Bloodhound对象时,可以将默认值更改为“ cache:false”和/或“ ttl:1000”(毫秒)。
预取是相关的,但与缓存略有不同,因为预取的数据不受服务器发送的Cache-Control标头的约束。 它也位于LocalStorage中,而不位于浏览器的缓存中(这就是硬重新加载或清除缓存不会导致重新获取它的原因)。
另一方面,远程提取的文件将根据Cache-Control标头进行重新验证。 因此,如果服务器允许,浏览器仍然可以缓存它们。 但是,它们存储在缓存中,而不是LocalStorage中。
LocalStorage中存在每个域空间的限制(请参阅localStorage值的最大大小是多少? ),所以大型预取将失败,尽管我不知道预输入是否会正常失败(即,即使使用数据,它无法存储它)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.