简体   繁体   中英

typeahead.js deduplicate between prefetch and remote datasources

I'm using typeahead.js with both prefetch and remote 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>')
    }       
});
});

However, there are duplicated entries in the prefetch JSON and remote JSON. How can I dedup so that it only shows one entry?

Add the dupDector option to your Bloodhound intitialisation code ie put the following code after "remote:" :

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

You haven't included your JSON so I cannot be sure that the comparison being made in the code above is correct. This code will ignore duplicate values in the local and remote datasources.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM