javascript - 无法理解如何在 typeahead 0.10.1 中使用 alt 模板引擎

标签 javascript mustache handlebars.js typeahead

我知道我可以使用 handlebars 来模拟使用 typeahead 的自定义模板样式。

$('#ItemLookup2').typeahead(null, {
    displayKey: 'description',
    source: values.ttAdapter(),
    templates: {
    suggestion: Handlebars.compile([
             '<p class="repo-language">{{Barcode}}</p>',
            '<p class="repo-name">{{sellPrice}}</p>',
            '<p class="repo-description">{{description}}</p>'
        ].join(''))
    }
});

这很好。

但是如果我想使用 Mustache.render 或 Hogan.compile 呢?

$('#ItemLookup2').typeahead(null, {
    displayKey: 'description',
    source: values.ttAdapter(),
    templates: {
        suggestion: Hogan.compile([
             '<p class="repo-language">{{Barcode}}</p>',
            '<p class="repo-name">{{sellPrice}}</p>',
            '<p class="repo-description">{{description}}</p>'
        ].join(''))
    }
});

抛出异常 TypeError: that.templates.suggestion is not a function.

我也试过

   var Suggestion = Mustache.render('<p class="repo-language">{{Barcode}}</p>');
$('#ItemLookup2a').typeahead(null, {
    displayKey: 'description',
    source: values.ttAdapter(),
    templates: {
            suggestion:Suggestion.join('')
    }
});

但是得到 TypeError: Suggestion.join is not a function

有人可以给我一些指示吗?

谢谢,

卡尔

最佳答案

这对我有用。

var templ =  Hogan.compile([
         '<p class="repo-language">{{Barcode}}</p>',
        '<p class="repo-name">{{sellPrice}}</p>',
        '<p class="repo-description">{{description}}</p>'
    ].join(''));

$('#ItemLookup2').typeahead(null, {
  displayKey: 'description',
  source: values.ttAdapter(),
  templates: {
    suggestion: function (data) { return templ.render(data); }
  }
});

查看源代码typeahead 10如何调用模板函数。 我只使用 getFooterHtml 片段,getSuggestionsHtml 的工作原理几乎相同,但传递不同的数据并做一些额外的工作。

            function getFooterHtml() {
                return that.templates.footer({
                    query: query,
                    isEmpty: !hasSuggestions
                });
            }

我们应该显式调用render 函数来渲染Hogan/Mustache。 Hogan.compile('sample {{data}}').render({"data": 'hey'}); 但是,Handlebars 可以像这样工作。 Handlebars.compile('sample {{data}}')({"data": 'hey'});

因此,Handlerbars 的问题片段 1 可以工作,但 Hogan/Mustache 应该做一些包装工作。

关于javascript - 无法理解如何在 typeahead 0.10.1 中使用 alt 模板引擎,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22248931/

相关文章:

javascript - 在 handlebars.js 中使用默认值切换大小写

javascript - Angular JS 游戏 - 使用方法创建和销毁子 Controller

javascript - 使用下划线组来构造我的数组

mustache - mustache.js 中的部分

typescript - 输入安全的 mustache 模板

editor - Notepad++ Dreamweaver 支持 Handlebar 和 Mustache js?

javascript - Angular 中的 D3 力模拟

javascript - Typescript 继承的父类中的参数是什么?

jquery - 如何在 Mustache.js 中使用比较来完成 if/else?

javascript - 数据未附加到 Handlebars.js 中的元素