Typeahead.js预取和远程数据源之间的重复数据删除

typeahead.js deduplicate between prefetch and remote datasources

本文关键字:数据 删除 之间 数据源 预取 js Typeahead      更新时间:2023-09-26

我使用typeahead.js预取和远程http://twitter.github.io/typeahead.js/examples/定制模板

$(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中存在重复的条目。我怎样才能使它只显示一个条目?

将dupDector选项添加到Bloodhound初始化代码中,即将以下代码放在"remote:"之后:

dupDetector: function(remoteMatch, localMatch) {
    return remoteMatch.value === localMatch.value;
}

你没有包括你的JSON,所以我不能确定在上面的代码中所做的比较是正确的。此代码将忽略本地和远程数据源中的重复值。