I've been struggling to make it work with JSON objects. Despite trying various solutions found on SO, none of them have worked for me.
$(function() {
var items = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
limit: 10,
prefetch: {
url: 'items.json',
filter: function(list) {
return $.map(list, function(item) {
return {
name: item.name,
category: item.category,
release: item.release,
id: item.id
};
});
}
}
});
items.initialize();
$('.typeahead').typeahead(null, {
name: 'items',
displayKey: 'name',
source: items.ttAdapter()
});
});
The JSON data is:
[
{"id":"4","name":"Name 1","release":"July 28, 2014","category":"Bow"},
{"id":"1","name":"Name 2","release":"October 29, 2014","category":"Bow"},
{"id":"13","name":"Name 3","release":"November 27, 2014","category":"Arrow"}
]