Question

I am using typeahead.js. I am using the prefetch options and need to parse the returned data. It's not throwing an error; it's not doing much of anything. I've looked for examples but none of them use the Prefetch Filter option.

Link to the prefetch documentation

My code (that does not work but throws no errors):

$('#autocomplete').typeahead('destroy').typeahead( {                                
    name: 'organizations',
    prefetch: {
        url: 'jsoncall',
        filter: function() {
                        // Blatant hardcoded return value
                        return ['test-a','test-b','test'c];
                      }
        }
    }
);

My HTML:

<input id="autocomplete" class="large-12" autocomplete="off" type="text" placeholder="Enter school name">

My Confusion:

0________0
Was it helpful?

Solution

You can pass the returned data through the filter function which then parses the data to your liking. So in your example above, you would do something like this:

$('#autocomplete').typeahead({                                
    name: 'organizations',
    prefetch: 
            {
        url: 'jsoncall',
        filter: function(data){
               // filter the returned data
               return [data.movies[0].title, data.movies[1].title, data.movies[2].title];
        }
    }
}
);

The example above would work if your returned data set was a JSON object that looked something like this:

movies: [{id:12865, title:Kill Bill: Volume 1, year:2003, mpaa_rating:R, runtime:111,…},…]
0: {id:12865, title:Kill Bill: Volume 1, year:2003, mpaa_rating:R, runtime:111,…}
1: {id:12862, title:Kill Bill, Volume 2, year:2004, mpaa_rating:R, runtime:137,…}
2: {id:771237417, title:Kill Bill: The Whole Bloody Affair, year:2011,     mpaa_rating:Unrated, runtime:,…}
3: {id:770998578, title:Angel of Death: Killer Nurse: A Bill Kurtis Special Report, year:2006,…}
4: {id:771352617, title:Kedi Billa Killadi Ranga, year:2013, mpaa_rating:Unrated, runtime:145,…}
total: 5
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top