0

I'm trying to use typeahead.js to get a list of authors from my own server (flask endpoint). When I use local source for Bloodhound everything works smoothly, only when I have to fetch the data from a server not all the results seem to be shown.

So this is the code:

var authors = new Bloodhound({
    datumTokenizer: Bloodhound.tokenizers.whitespace,
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    // local: ["Aad, Georges", "Abajyan, Tatevik", "Abbott, Brad"],

    remote: {
        url: '/search/authors?q=%QUERY',
        transform: function (json) {
            var names = $.map(json.results, function (author) {
                return author.full_name;
            });
            console.log(names);
            return names;
        }
    }
});

$('#author-suggest').typeahead(null, {
    source: authors
});

I make sure to print the names that come back from the url and they seem to be identical to what I might put into the local variable:

$ Array [ "Aad, Georges", "Abajyan, Tatevik", "Abbott, Brad" ]
$ Array [ "Aad, Georges", "Abajyan, Tatevik", "Abbott, Brad" ]
…

Yet something strange happens when I test it. When I try to type in the input box - I get suggestions, however it's only a subset of those printed in the console. They also don't dynamically change when I type (Aad, Georges is still the first suggestion even when I'm already at "Abbott").

The other peculiar thing is that not all of the names are being suggested, even though all of them have clearly came back from the server. When the server sends 3 names, only the first 2 are in the suggestions. When it sends 4 names, 1 suggestion appears. The general pattern is:

[(1 -> 1), (2 -> 2), (3 -> 2), (4 -> 1), (5 -> 0), (6 -> 5), (7 -> 5)… ]

Where the first number in a pair represents the number of authors received and the second is the number of suggestions. What the heck is going on here?!

Thanks in advance!

Community
  • 1
  • 1
stpk
  • 2,015
  • 1
  • 16
  • 23
  • I'm seeing something similar with remote data (haven't tested local), and it's driving me bonkers. You ever sort this out? – mlissner Dec 02 '15 at 19:46
  • I think I finally solved it, but don't remember how. Try adding `wildcard: '%QUERY'` to the `remote` Bloodhound field and `valueKey: 'symbol'` in the typeahead object definition (next to the `source` field). Maybe one of these'll help – stpk Dec 03 '15 at 14:28

0 Answers0