I am having trouble getting typeahead.js to return only results that match the query I entered. For example, if I type “Facebook” in the company search bar, it will return all the companies (“Yahoo”, “Google”, etc.), although most of them do not match the query. I do not process data on the server side. Should my datumTokenizer function take care of this filtering?
In addition, I notice that every time I change the request, it introduces a filter () function for each base element. Therefore, when I change the request from "G" to "Go", the console.log () operator in the filter: function (company_list) function will print 3,000 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) {
return $.map(companies_list, function (company) {
console.log('mapping')
return {
name: company.name
};
});
}
}
});
var promise = companies.initialize();
promise.done(function() {console.log('Bloodhound initialized!')});
$('#form-company').typeahead(null, {
name: 'companies',
displayKey: 'name',
source: companies.ttAdapter()
});
And an example of returning my URL:
[{"name": "Yahoo"}, {"name": "Sanchai Technologies "}, {"name": "Oliver Wyman"}, {"name": "University of Oregon"}, ...]
remote, prefetch . [object Object], . prefetch/remote .json . , - ( , 77 ), .
!