2014-06-05 37 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 –