【问题标题】:Compiling html templates using handlebars.js in backbone marionette CollectionView在骨干木偶 CollectionView 中使用 handlebars.js 编译 html 模板
【发布时间】:2013-09-06 04:53:16
【问题描述】:

我正在使用带有 handlebars.js 和 require.js 的骨干木偶。因此,如果我们不必提供任何动态值,我知道如何使用模板。

但现在我必须使用木偶框架的 CollectionView 显示员工列表所以现在我无法理解如何使用把手向 Collection View 提供动态数据。

这是我的看法

ContactManager.module('ContactsApp.List', function(List, ContactManager,
        Backbone, Marionette, $, _) {

    List.Contact = Marionette.ItemView.extend({
        tagName : "li",
        template : Handlebars.compile(EmployeeTemplate)
    });

    List.Contacts = Marionette.CollectionView.extend({
        tagName : "ul",
        itemView : List.Contact
    });
});  

这是模板

<label><%= firstName %> <%= lastName %> </label>

如何将这些参数动态地提供给 CollectionView?

【问题讨论】:

    标签: jquery html marionette collectionview handlebars.js


    【解决方案1】:

    查看有关 Renderer 和 TemplateCache 的 Marionette 文档。在这两种资源之间,您应该能够做到这一点。我个人使用 Mustache,并覆盖了文档中指定的方法……而且效果很好。

    首先是渲染器:

    https://github.com/marionettejs/backbone.marionette/blob/master/docs/marionette.renderer.md#custom-template-selection-and-rendering

    基本上,覆盖Renderer.render method。它将接收模板 ID(您在模型上指定的内容和数据)。它应该返回渲染的模板。

    然后是 TemplateCache:

    https://github.com/marionettejs/backbone.marionette/blob/master/docs/marionette.templatecache.md#customizing-template-access

    TemplateCache 可用于通过loadTemplate 检索模板,然后使用compileTemplate 编译模板。覆盖这些以指向 Handlebars,然后离开!

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多