Night Train
Night Train

Reputation: 781

Twitter Typeahead With Prefetched JSON

So I'm attempting to use Twitter's Typeahead auto-complete library. I prefetch some JSON data that I have and subsequently put the information I need into an array. My code for this can be seen below:

var test = new Bloodhound({
    datumTokenizer: function(d) { return Bloodhound.tokenizers.whitespace(d.name); },
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    limit: 10,
    prefetch: {
    url: 'http://localhost:3000/suggestions',
      filter: function(list) {
        nameArray = [];
        for( var i = 0; i < list.length; ++i){
          nameArray.push(list[i].name);
        }
        window.alert(nameArray)
        return nameArray;
      }
    }
});

test.initialize();

$('.example-test .typeahead').typeahead(null, {
    name: 'test',
    source: test.ttAdapter()
});

I used the alert to make sure the array is what I wanted it to be, which is simply an array of strings.

The above code does not seem to produce anything though. I get no autocomplete functionality. The array is generated correctly, so I'm not sure what the issue is. I've tested my HTML and CSS using the examples Twitter provides and they work flawlessly. Any help is greatly appreciated.

Upvotes: 1

Views: 500

Answers (1)

Andrew Templeton
Andrew Templeton

Reputation: 1696

Race condition - you need to only apply the typeahead call after success on the ajax request.

Put the last block inside a success function and you should be good.

Upvotes: 1

Related Questions