Reputation: 24305
I've played around with every setting I can think of, e.g., .clear()
and .typeahead('destroy')
, and once I've set the source as remote I can't make the typeahead use a local source.
Any thoughts?
Here's the code below that gets called onclick
:
var create_typeahead = function(is_remote, titles){
filters_typeahead.typeahead('destroy');
if(is_remote){
var remote_url = titles;
var titles = new Bloodhound({
queryTokenizer: Bloodhound.tokenizers.whitespace,
datumTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: remote_url,
q=%QUERY',
wildcard: '%QUERY'
}
});
}
else{
var titles0 = titles;
var titles = new Bloodhound({
queryTokenizer: Bloodhound.tokenizers.whitespace,
datumTokenizer: Bloodhound.tokenizers.whitespace,
local: titles0
});
}
titles.initialize();
filters_typeahead.typeahead({
highlight: true,
minLength: 2,
},
{
name: 'titles',
displayKey: 'name',
source: titles,
templates: {
suggestion: function(data) {
return '<div>' + data.name + '</div>';
}
}
});
};
Upvotes: 0
Views: 898
Reputation: 1678
My answer is a little more involved than yours, but hopefully it will point you in the right direction.
When you want to change a remote
source using bloodhound
, you will have to clear the bloodhound object and reinitialize it.
Here I am creating an initializing a bloodhound instance:
var taSource = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('Value'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
identify: function(obj) {
return obj.Value;
},
remote: remoteSrc
});
taSource.initialize(true);
In the logic to switch I call both clear()
and initialize(true)
, passing true for the reinitialize paramter:
taSource.clear();
taSource.initialize(true);
I handle changing the URL in the prepare
method that is available as part of the remote
object.
prepare: function(query, settings) {
settings.url = urlRoot + '/' + whichType + '?q=' + query;
console.log(settings.url);
return settings;
},
Here is a full example show how I handle it.
Upvotes: 1