admin管理员组文章数量:1026216
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([
{
name: "pages"
,local: localSuggestions
,template: '<div class="tt-suggest-page">{{value}}</div>'
,engine: Hogan
}
]);
According to the Migration Guide to 0.10 "Prepiled Templates are Now Required", so in 0.10.3 I'm trying:
$('input.search-query').typeahead(null, {
name: "pages"
,source: taSourceLocal.ttAdapter()
,templates: {
suggestion: Hoganpile('<div class="tt-suggest-page">{{value}}</div>')
}
});
but it does not work. I get an error: Uncaught TypeError: object is not a function
If there is another, minimalist template engine that can work I will consider it as well, but it has to be small. I don't want to add a huge file like Handlebars or a whole library like Underscore.
any ideas? TIA!
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([
{
name: "pages"
,local: localSuggestions
,template: '<div class="tt-suggest-page">{{value}}</div>'
,engine: Hogan
}
]);
According to the Migration Guide to 0.10 "Prepiled Templates are Now Required", so in 0.10.3 I'm trying:
$('input.search-query').typeahead(null, {
name: "pages"
,source: taSourceLocal.ttAdapter()
,templates: {
suggestion: Hogan.pile('<div class="tt-suggest-page">{{value}}</div>')
}
});
but it does not work. I get an error: Uncaught TypeError: object is not a function
If there is another, minimalist template engine that can work I will consider it as well, but it has to be small. I don't want to add a huge file like Handlebars or a whole library like Underscore.
any ideas? TIA!
Share Improve this question edited Jul 13, 2014 at 23:25 isapir asked Jul 13, 2014 at 22:38 isapirisapir 23.8k16 gold badges125 silver badges121 bronze badges1 Answer
Reset to default 10As stated by Jake Harding, the solution for modern browsers is like so:
var piledTemplate = Hogan.pile('<div class="tt-suggest-page">{{value}}</div>');
$('input.search-query').typeahead(null, {
// ...
templates: {
suggestion: piledTemplate.render.bind(piledTemplate);
}
});
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>';
};
}
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([
{
name: "pages"
,local: localSuggestions
,template: '<div class="tt-suggest-page">{{value}}</div>'
,engine: Hogan
}
]);
According to the Migration Guide to 0.10 "Prepiled Templates are Now Required", so in 0.10.3 I'm trying:
$('input.search-query').typeahead(null, {
name: "pages"
,source: taSourceLocal.ttAdapter()
,templates: {
suggestion: Hoganpile('<div class="tt-suggest-page">{{value}}</div>')
}
});
but it does not work. I get an error: Uncaught TypeError: object is not a function
If there is another, minimalist template engine that can work I will consider it as well, but it has to be small. I don't want to add a huge file like Handlebars or a whole library like Underscore.
any ideas? TIA!
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([
{
name: "pages"
,local: localSuggestions
,template: '<div class="tt-suggest-page">{{value}}</div>'
,engine: Hogan
}
]);
According to the Migration Guide to 0.10 "Prepiled Templates are Now Required", so in 0.10.3 I'm trying:
$('input.search-query').typeahead(null, {
name: "pages"
,source: taSourceLocal.ttAdapter()
,templates: {
suggestion: Hogan.pile('<div class="tt-suggest-page">{{value}}</div>')
}
});
but it does not work. I get an error: Uncaught TypeError: object is not a function
If there is another, minimalist template engine that can work I will consider it as well, but it has to be small. I don't want to add a huge file like Handlebars or a whole library like Underscore.
any ideas? TIA!
Share Improve this question edited Jul 13, 2014 at 23:25 isapir asked Jul 13, 2014 at 22:38 isapirisapir 23.8k16 gold badges125 silver badges121 bronze badges1 Answer
Reset to default 10As stated by Jake Harding, the solution for modern browsers is like so:
var piledTemplate = Hogan.pile('<div class="tt-suggest-page">{{value}}</div>');
$('input.search-query').typeahead(null, {
// ...
templates: {
suggestion: piledTemplate.render.bind(piledTemplate);
}
});
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>';
};
}
本文标签: javascriptMigrating to Typeahead 010 with HoganStack Overflow
版权声明:本文标题:javascript - Migrating to Typeahead 0.10+ with Hogan - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745631802a2160209.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论