Integrating iCanHaz and Marionette

和自甴很熟 提交于 2019-11-28 06:43:58

问题


I'm a big fan of ICanHaz, and I'm trying to directly intregrate it into a new Marionette application I'm building. However, going off this post, I have written this that reaches into the render method and changes it in 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();

    });

If I walk through this function, all the data seems to work fine. However, when put into use and trying to use it for layouts and Item Views, nothing gets appended or inserted. This is from my 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);

This all happens in a method that gets called when index is hit. There are no JS errors so I have nothing to go on. However, it in the render function I append the data to the body, it will add (however ruining my layout and region structure).

I am storing my templates in index.html.

Can anyone help with this?


回答1:


Okay, I couldn't find an easy way to do this using ICH. However, due to another SO I found, very similar functionality can be found just using Mustache.

Using this code:

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

Lets you change the renderer so you can pull mustache templates from index.html using Marionette's template call. A mustache template looks like this:

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

The difference is that the type is 'text/template' as opposed to 'text/html'. Otherwise it acts very similar.

Hope this helps someone else.



来源:https://stackoverflow.com/questions/15640168/integrating-icanhaz-and-marionette

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!