I'm having trouble getting typeahead.js to return only results that match the inputted query. For example, if I type in "Facebook" to my company search bar, it will return all companies ("Yahoo", "Google", etc.) even though most of these don't match the query. I am not doing any server side processing of data. Should my datumTokenizer function take care of this filtering?
Also, I notice that every time I modify the query, it enters the filter() function for each datum. So when I change the query from "G" to "Go," the console.log() statement in filter: function (companies_list) will print 3000 times.
Here is my code:
var companies = new Bloodhound({
datumTokenizer: function (datum) {
return Bloodhound.tokenizers.whitespace(datum.name);
},
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: '/json/company_list.json',
filter: function (companies_list) {
// Map the remote source JSON array to a JavaScript object array
return $.map(companies_list, function (company) {
console.log('mapping')
return {
name: company.name
};
});
}
}
});
// Initialize the Bloodhound suggestion engine
var promise = companies.initialize();
promise.done(function() {console.log('Bloodhound initialized!')});
// passing in `null` for the `options` arguments will result in the default
// options being used
$('#form-company').typeahead(null, {
name: 'companies',
displayKey: 'name',
// `ttAdapter` wraps the suggestion engine in an adapter that
// is compatible with the typeahead jQuery plugin
source: companies.ttAdapter()
});
And an example of what my url is returning:
[{"name": "Yahoo"}, {"name": "Sanchai Technologies "}, {"name": "Oliver Wyman"}, {"name": "University of Oregon"}, ...]
I am using remote because prefetch absolutely does not work for me. It only gives me the suggestion [object Object], which makes no sense. I would like to use prefetch/remote to load the entire .json file upon initialization, and not make any further requests to the server. So I think prefetch is the better option of me (small file, 77kB), but it's just not working at all.
Thanks v much for the help!