问题
I can't get it to work with JSON objects. I've followed multiple questions here on SO and none of the answers helped 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()
});
});
JSON is as:
[
{"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"}
]
回答1:
The reason it wouldn't work is was because of Typeahead's LocalStorage usage (or perhaps my abuse of it, I guess we'll never know). As soon as I cleared that, it instantly worked.
来源:https://stackoverflow.com/questions/26027474/twitters-typeahead-bloodhound-with-json-objects