2014-02-19 29 views
3

我在應用程序中使用typeahead(0.10.1)來返回使用血峯預取的客戶端列表。我無法解決如何使用新的指紋更新/重新初始化獵犬,這樣在插入新的客戶端後,它將使用新的指紋,因此包含新的客戶端。初始加載後無法更新twitter typeahead.js預取指紋

該系統是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提到拉入請求更新我的預輸入文件後,我可以得到它復位,但不reinitialise.Using如下:

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

不能重新初始化警犬的權利,但這種能力將在V0存在。 10.2。這裏是相關的拉請求:https://github.com/twitter/typeahead.js/pull/703 – jharding

+0

太棒了!我抓住了這個拉取請求,並用grunt作爲測試更新了我的文件。我可以重置它,但我似乎無法重新初始化它: – MrTomTom

+0

嘗試在重新初始化時將'true'傳遞給初始化方法。我仍在努力通過我想要的API,如果您有任何想法,您應該將它們發佈到請求中。 – jharding

回答

7

你可能有一個答案,這已經但我想我要補充的是爲我工作的代碼

//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); 

希望它可以幫助

0

以我爲例,我從我的網址結果使用JSON數據預取, ,想只清除預取數據(因爲我需要添加一些其他的數據後,將其清除)

clients.initialize(true);方法爲我工作