Dennington-bear Dennington-bear - 1 month ago 8
Javascript Question

bloodhound resetting local data

Im using bloodhound and trying to reset the

local
option. on my webpage I have a situation whereby If the user chooses one of two options the suggestion data will change. But I cant get the suggestions to appear when I set the local data. Below is how I create the bloodhound setting it up to use an array called 'spray':

var dataSetBloodhound = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
local: spray
});


So if the user chooses spray jQuery catches it and tries to set the spray array to the local

//If spray is picked
if( checked == "Spray") {
// Clear the object
dataSetBloodhound.clear();
// Set the local 'data' to spray
dataSetBloodhound.local = spray;
// Reinit the object
dataSetBloodhound.initialize()
// Prints out the local data.(This prints out the data of spray)
console.log(dataSetBloodhound.local);
//etc }


If the user chooses fertilizer the function is the same as above except a
fertilizer
array is passed.
If I use on list i can get them to work but I cant seem to swap the local data of bloodhound. Yet after I
initialize()
I can
console.log(dataSetBloodhound.local)
and I can view the elements in the console but if I try and get a suggestion from the dropdown it doesnt work. Any ideas?

Answer

I think you need to reinitialize, you can set

if( checked == "Spray") {
        // Clear the object
        dataSetBloodhound.clear();
        // Set the local 'data' to spray
        dataSetBloodhound.local = spray;
        // Reinit the object
        dataSetBloodhound.initialize(true);
        // Prints out the local data.(This prints out the data of spray)
        console.log(dataSetBloodhound.local);

Please check the documentation https://github.com/twitter/typeahead.js/blob/master/doc/bloodhound.md