Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 869
Ian Lin Avatar asked Mar 10 '26 10:03

Ian Lin


1 Answers

Add the dupDector option to your Bloodhound intitialisation code i.e. 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.

like image 152
Ben Smith Avatar answered Mar 13 '26 00:03

Ben Smith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!