Migrating to Typeahead 0.10+ with Hogan

前端 未结 1 2082
夕颜
夕颜 2020-12-19 14:28

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         


        
相关标签:
1条回答
  • 2020-12-19 14:47

    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 engine
                return '<div class="tt-suggest-page">' + data.value + '</div>';
            };
        }
    
    0 讨论(0)
提交回复
热议问题