Twitter's typeahead + Bloodhound with JSON objects

╄→гoц情女王★ 提交于 2019-12-07 09:38:20

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!