typeahead.js如何在预取数据源和远程数据源之间去重。

3

我正在使用同时具有预取和远程功能的typeahead.js。详情请参考http://twitter.github.io/typeahead.js/examples/#custom-templates

$(document).ready(function() {
var castDirectors = new Bloodhound({
  datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  prefetch: '../api/v1/search/people_typeahead',
  remote: '../api/v1/search/people_typeahead?q=%QUERY'
});

castDirectors.initialize();

$('#remote .typeahead').typeahead(null, {
  name: 'cast-directors',
  displayKey: 'value',
  source: castDirectors.ttAdapter(),
    templates: {
        empty: [
      '<div class="empty-message">',
      'no matching names',
      '</div>'
    ].join('\n'),
        suggestion: Handlebars.compile('<p><a href="{{link}}">{{value}}</a></p>')
    }       
});
});

然而,在预取JSON和远程JSON中存在重复的条目。如何去重,使其仅显示一个条目?

1个回答

4
在您的Bloodhound初始化代码中添加dupDector选项,即在“remote:”后面放置以下代码:
dupDetector: function(remoteMatch, localMatch) {
    return remoteMatch.value === localMatch.value;
}

您没有包含JSON,因此我无法确定上面代码中进行的比较是否正确。此代码将忽略本地和远程数据源中的重复值。


没有问题 @IanLin。很高兴我能帮到你 :) - Ben Smith
1
这个还是可选项吗?我在其他地方除了旧的gists和SO帖子之外找不到任何文档。更新:这已被identify取代:https://github.com/twitter/typeahead.js/issues/946 - Peter P.
@PeterP。是的,我的回答是针对较旧版本的Bloodhound。感谢您提供有用的更新。 - Ben Smith

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接