Skip to content Skip to sidebar Skip to footer

Migrating To Typeahead 0.10+ With Hogan

I have been using Typeahead 0.9.3 with Hogan 2 for a while and it was very straight forward to setup. in 0.9.3 I did something like: $('input.search-query').typeahead([ {

Solution 1:

As stated by Jake Harding, the solution for modern browsers is like so:

var compiledTemplate = Hogan.compile('<div class="tt-suggest-page">{{value}}</div>');

$('input.search-query').typeahead(null, {
    // ...templates: {
        suggestion: compiledTemplate.render.bind(compiledTemplate);
    }
});

Unfortunately, Function.prototype.bind() is not supported by IE < 9, so if you need to support older browsers that will not work.

The good news is that as stated by Steve Pavarno you don't need a template engine anymore. You can achieve the desired result by passing a function like so:

// ...
    templates: {
        suggestion: function(data) { // data is an object as returned by suggestion enginereturn'<div class="tt-suggest-page">' + data.value + '</div>';
        };
    }

Post a Comment for "Migrating To Typeahead 0.10+ With Hogan"