0

我是 ICanHaz 的忠实粉丝,我正在尝试将它直接集成到我正在构建的新 Marionette 应用程序中。然而,从这篇文章开始,我已经写了这个进入渲染方法并在 Marionette 中更改它:

// Set up Initalizer
    APP.addInitializer(function() {

        //Reach into Marionette and switch out templating system to ICH
        Backbone.Marionette.Renderer.render = function(template, data){
            return ich[template](data);
        }

        //Create Router
        new APP.Routers.GlobalRouter();

        //Start Backbone History
        Backbone.history.start();

    });

如果我浏览此功能,所有数据似乎都可以正常工作。但是,当投入使用并尝试将其用于布局和项目视图时,没有任何内容被附加或插入。这是来自我的 GlobalRouter:

 //Grab the main Layout
        var layout = new APP.Views.LayoutView();

        //Render that layout
        layout.render();


        //Make the model
        var userModel = new APP.Models.UserModel({
          "user_name" : "nweingartner@awesome.com",
          "tenant" : "Ginger Ale is Great"
        });

        //Make the Header Region
        var headerRegion = new APP.Views.HeaderView({model: userModel});
        layout.header.show(headerRegion);

这一切都发生在一个在索引被命中时被调用的方法中。没有 JS 错误,所以我无事可做。但是,在渲染函数中,我将数据附加到正文中,它会添加(但是破坏了我的布局和区域结构)。

我将模板存储在 index.html 中。

有人能帮忙吗?

4

1 回答 1

1

好的,我找不到使用 ICH 的简单方法。但是,由于我发现了另一个 SO,仅使用 Mustache 就可以找到非常相似的功能。

使用此代码:

 Backbone.Marionette.TemplateCache.prototype.compileTemplate = function(rawTemplate) {
    return Mustache.compile(rawTemplate);
 }

允许您更改渲染器,以便您可以使用 Marionette 的模板调用从 index.html 中提取 mustache 模板。小胡子模板如下所示:

 <script id="headerTemplate" type="text/template">
        <p>{{user_name}}</p>
        <p>{{tenant}}</p>
    </script>

不同之处在于类型是“text/template”而不是“text/html”。否则它的行为非常相似。

希望这对其他人有帮助。

于 2013-03-27T12:40:35.203 回答