2014-06-05 29 views
1

我能夠使用舊版本的Typeahead,沒有問題,我對新版本的瞭解是有限的,而且我對於爲什麼會出現重複的條目感到困惑。Twitter Typeahead - 重複的AJAX建議

這是我的javascript:

// Sources 
var sources = new Bloodhound({ 
    datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'), 
    queryTokenizer: Bloodhound.tokenizers.whitespace, 
    prefetch: '/sources/prefetch/', 
    remote: '/sources/prefetch/' 
}); 

sources.initialize(); 

$('#a_sources_list').typeahead(null, { 
    name: 'sources', 
    displayKey: 'name', 
    source: sources.ttAdapter() 
}) 

/來源/預取/回報:

[{"id":"1","name":"Google"},{"id":"3","name":"Yahoo"}] 

這裏是正在發生的截圖:Duplicate Results

回答

5

問題同從同一來源調用prefetchremote

的問題在這裏詳細描述:
https://github.com/twitter/typeahead.js/issues/614

從本質上講,警犬有一個默認的限制。如果建議數量低於此限制,則會調用遠程URL。

還有就是要創建一個重複的檢測器的選項:
https://github.com/twitter/typeahead.js/blob/master/doc/bloodhound.md#options

你可以用這個來確保同樣的項目不會出現兩次。

這裏有一個dupDetector的例子:
https://github.com/twitter/typeahead.js/issues/606#issuecomment-34667422

dupDetector: function(remoteMatch, localMatch) { 
    return remoteMatch.id === localMatch.id; 
} 
+0

如果我初始化我警犬實例與本地而非預取功能或遙控器,我會永遠dupDetector被稱爲? –

+0

我不知道,我會把一個console.log中,並找出。 –

+0

謝謝你的建議。我認爲問題是我沒有使用遠程或預取。 https://github.com/twitter/typeahead.js/issues/606#issuecomment-51221195 –