2013-03-26 32 views
0

我是ICanHaz的忠實粉絲,我試圖直接將它整合到我正在構建的新的Marionette應用程序中。然而,去關this post,我寫了這個到達到渲染方法,並改變它的提線木偶:集成iCanHaz和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" : "[email protected]", 
      "tenant" : "Ginger Ale is Great" 
     }); 

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

這一切都發生在索引被命中時調用的方法中。沒有JS錯誤,所以我沒有什麼可繼續。然而,它在渲染函數中追加數據到正文,它會添加(但是破壞我的佈局和區域結構)。

我在index.html中存儲我的模板。

任何人都可以幫忙嗎?

回答

1

好吧,我找不到使用ICH的簡單方法。但是,由於我發現了另一個SO,使用Mustache可以找到非常類似的功能。

使用此代碼:

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

允許您更改呈現,因此您可以從index.html的使用木偶的模板調用鬍子拉模板。鬍鬚模板看起來是這樣的:

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

不同的是,該類型是「文本/模板」,而不是「text/html的」。否則它的行爲非常相似。

希望這可以幫助別人。