MrT*_*Tom 3 javascript typeahead.js twitter-bootstrap-3 bloodhound
我在应用程序中使用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()
})
Run Code Online (Sandbox Code Playgroud)
任何人都有任何想法,我做错了什么?
编辑:使用@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;
}
Run Code Online (Sandbox Code Playgroud)
小智 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);
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你
| 归档时间: |
|
| 查看次数: |
2776 次 |
| 最近记录: |