I'm using typeahead.js and would like to use a local array for the typeahead if the user has typed 3 or less characters, and then fall back onto a remote query if typing more than that. How can I implement an if
statement in the source
option of the typeahead? Below is my code now, but it's not working as intended. The source doesn't seem to like this method. When I call localTypeaheadData
it doesn't get returned properly to the source.
var localTypeaheadData = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.whitespace,
queryTokenizer: Bloodhound.tokenizers.whitespace,
local: localArray
});
$('#sitesearch').typeahead({
minLength: 1,
},
{
name: 'sitesearch',
source: function (query, process) {
if (query.length < 4) {
return localTypeaheadData;
} else {
return remoteTypeaheadData;
}
}
Copyright Notice:Content Author:「Tom Freezers」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/30770378/twitter-typeahaed-js-change-query-source-when-certain-characters-are-hit