Twitter的typeahead + Bloodhound与JSON对象

时间:2014-09-24 22:17:27

标签: javascript json typeahead bloodhound

我无法使用JSON对象。我在这里关注过多个问题,没有一个答案对我有帮助。

$(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如下:

[
 {"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 个答案:

答案 0 :(得分:3)

它不起作用的原因是因为Typeahead的LocalStorage使用(或者我滥用它,我想我们永远不会知道)。一旦我清除了它,它立即起作用。