我在app中使用typeahead(0.10.1)来返回使用bloodhound预取的客户端列表。我无法弄清楚如何使用新的指纹更新/重新初始化猎犬,以便在插入新客户端后,它将使用新的指纹,因此包括新的客户端。
系统是ajax所以我不能只在刷新时更改名称,我试图在reinitializeclient()中按照下面的方式传递另一个指纹,但是它不起作用:
var clientthumbprint = "initialname";
var clients = new Bloodhound({
limit: 5,
prefetch:{
url: '/urltofeed/',
thumbprint:getthumbprint()
},
datumTokenizer: function(d) {
return Bloodhound.tokenizers.whitespace(d.label);
},
queryTokenizer: Bloodhound.tokenizers.whitespace
});
clients.initialize();
function reinitializeclient(newthumbprint){
//This is called when a client is saved
clientthumbprint = newthumprint;
clients.initialize(); //this didn't work
}
function getthumbprint(){
return clientthumbprint;
}
$('#search_user').typeahead({
minLength: 2,
},
{
displayKey: 'label',
source: clients.ttAdapter()
})
任何人都有任何想法,我做错了什么?
编辑:使用@jharding提到的拉取请求更新我的先行文件后,我可以重置它,但不能重新初始化。使用以下内容:
function initialize_clients(){
clients = new Bloodhound({
limit: 5,
prefetch:{
url: '/pathtojson/',
thumbprint:getthumbprint()
},
datumTokenizer: function(d) {
return Bloodhound.tokenizers.whitespace(d.label);
},
queryTokenizer: Bloodhound.tokenizers.whitespace
});
clients.initialize();
};
function resetclients(){
clientthumbprint = 'somenewthumbprint';
clients.reset();//this works - after running nothing will return in search
initialize_clients();//this is not working
}
function getthumbprint(){
return clientthumbprint;
}
答案 0 :(得分:8)
你可能已经有了这个答案,但我想我会添加适合我的代码
//This seems to refresh my prefetch data
clients.clearPrefetchCache();
//not sure whether setting to true does anything
//though, but according to the bloodhound.js it should force a reinitialise
clients.initialize(true);
希望有所帮助
答案 1 :(得分:1)
就我而言,我使用prefetch从url获取结果作为JSON数据, 并且只想清除预取数据(因为我需要在添加其他数据后清除它)
clients.initialize(true);
方法为我工作
答案 2 :(得分:1)
这始终对我有用,以清除本地/预取/远程数据并重新加载:
clients.clear();
clients.clearPrefetchCache();
clients.clearRemoteCache();
clients.initialize(true);