twitter / typeahead.js

typeahead.js is a fast and fully-featured autocomplete library
http://twitter.github.io/typeahead.js/
MIT License
16.52k stars 3.2k forks source link

JSON Remote doesn't work #1362

Open Revyn112 opened 9 years ago

Revyn112 commented 9 years ago

Hello,

I tried to realize an autocomplete feature for the search of address via the API of http://photon.komoot.de. Here is my code snippet, but I get this simply not to run and did not see my mistake. I would be grateful, if you can help me with my problem.

var places = new Bloodhound({
    datumTokenizer: function (datum) {
        return Bloodhound.tokenizers.whitespace(datum.value);
    },
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    remote: {
        url: 'http://photon.komoot.de/api/?q={query}&limit=5&lang=de',
        wildcard: '{query}',
        filter: function (places) {
            console.log(places);
            return $.map(places.features, function (place) {
                console.log('LOG:'+ place.properties.name);
                return {
                    value: place.properties.name
                };
            });
        }
    }
});

$('.typeahead').typeahead({
    hint: true,
    highlight: true,
    minLength: 1
},{
    name: 'places',
    display: 'value',
    source: places
});
ImranAhmed commented 9 years ago

I haven't tried the above code but I just looking at it you are missing the initialise as follows: . . . // Initialize the Bloodhound suggestion engine places.initialize();

$('.typeahead').typeahead({ hint: true, highlight: true, minLength: 1 },{ name: 'places', display: 'value', source: places });

see usage:https://github.com/twitter/typeahead.js/blob/master/doc/bloodhound.md#bloodhoundinitializereinitialize

lesterchan commented 9 years ago

@Revyn112: Try transform() instead of filter() @ImranAhmed in the docs it writes: Note, unless the initialize option is false, this method is implicitly called by the constructor.

Revyn112 commented 9 years ago

@lesterchan Thank you, your solution work with up to 4 items in an array, but i get just one suggestion displayed and is an array of 5 items or more given, typeahead don't work anymore. Can you help me just one more time? :)

typeahead .