Why does bloodhound.get() return undefined?

11,271

I implemented Bloodhound.get() as follows (also see this fiddle : http://jsfiddle.net/Fresh/HS9Wy/):

// instantiate the bloodhound suggestion engine
var numbers = new Bloodhound({
    datumTokenizer: function (d) {
        return d;
    },
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    local: ["(A)labama", "Alaska", "Arizona", "Arkansas"]
});

// initialize the bloodhound suggestion engine
numbers.initialize();

// Get an array of datums which satisfy the query for 'a'
numbers.get('a', function (suggestions) {
    jQuery.each(suggestions, function (index, item) {
        console.log(item);
    });
});

The problem with your call to "get()" i.e.

numbers.get('a')

Is that whilst you are getting Bloodhound to execute the query for 'a' you are not doing anything with the results. To instruct "get()" to do something useful you need to send the results to the output function. See the documentation here.

Share:
11,271

Related videos on Youtube

Bass Jobsen
Author by

Bass Jobsen

Author of Less Web Development Essentials http://www.packtpub.com/less-web-development-essentials/book and Less Web Development Cookbook https://www.packtpub.com/web-development/less-web-development-cookbook. Currently, i write a blog (http://bassjobsen.weblogs.fm/), program LBS for mobile devices (http://www.gizzing.nl), make cool websites (such as http://www.streetart.nl/), and counsel in setting up the technical environments and requirements. You can also check out my Bootstrap WordPress Starters Theme (JBST) and other projects at GitHub at https://github.com/bassjobsen. Also try: http://twitterbootstrap3buttons.w3masters.nl/ and http://twitterbootstrap3navbars.w3masters.nl/ Twitter: @bassjobsen Google+: http://google.com/+BassJobsen

Updated on June 04, 2022

Comments

  • Bass Jobsen
    Bass Jobsen almost 2 years

    I'm trying to use the code below with typeahead.js v 0.10

    // instantiate the bloodhound suggestion engine
    var numbers = new Bloodhound({
    datumTokenizer: function(d) { return d; },
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    local:  ["(A)labama","Alaska","Arizona","Arkansas"]
    });
    
    // initialize the bloodhound suggestion engine
    numbers.initialize();
    console.log(numbers.get('a'));
    

    In fact I try to solve this question: https://github.com/bassjobsen/Bootstrap-3-Typeahead/issues/26 I expected something like shown below should be possible:

     $('.typeahead').typeahead(
    {
    items: 4,
    source:function(query){return numbers.get(query)}       
    });
    

    update

    The examples. use ttAdapter() to set the source of typeahead. This function can also be used to set the source property (which accept an array of string or a function) for Bootstrap-3-Typeahead:

    // instantiate the bloodhound suggestion engine
    var numbers = new Bloodhound({
    datumTokenizer: Bloodhound.tokenizers.whitespace,//function(d) { return d; },
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    local:  ["(A)labama","Alaska","Arizona","Arkansas","Arkansas2","Barkansas"]
    });
    
    // initialize the bloodhound suggestion engine
    numbers.initialize();
    
    $('.typeahead').typeahead(
    {
    items: 4,
    source:numbers.ttAdapter()  
    });
    

    bloodhound.js shows:

      ttAdapter: function ttAdapter() {
                    return _.bind(this.get, this);
                }
    

    So ttAdapter() returns a function (get()) which can be set by source which has the query as an argument.

  • Bass Jobsen
    Bass Jobsen about 10 years
    Thanks for your answer. But in fact this still doesn't return anything?? I finally found from the examples twitter.github.io/typeahead.js/examples. ttAdapter() return a function which can be used to set the source of Bootstrap-3-Typeahead
  • Ben Smith
    Ben Smith about 10 years
    Correct, "get()" doesn't return anything. Your question specifically asked why "get()" doesn't return anything, and I explained that you were using it incorrectly. Glad that you found ttAdapter after looking at the basic examples.