2015-09-24 23 views
0

因此,我使用骨幹模型呈現HBS模板。我想創建一套諧音的UI組件的,我可以用不同的對象調用生成標記如此:調用單獨的對象作爲Handlebars部分的上下文

<div class="device"> 
    This is my device default name: {{ defaultName }} <br/> 
    This is my device current state: {{ currentState }} <br/> 
    This is my device last state: {{ lastState }} 
    {{> options}} 
</div> 

和部分是:

{{#each options}} 
<span data-value="{{value}}">Label: </span>{{label}} <br/> 
{{/each}} 

我想在主模板一切使用模型作爲上下文,部分使用我在視圖中創建的不同對象。我試圖避免讓這些選項與我的模型代碼結婚,yuck。

更新 我做了一點工作,周圍是沒關係與$ .extend

var data = $.extend({}, this.model.toJSON(), this.uiOptions); 
this.$el.html(this.template(data)); 

回答

1

一個做到這一點的最巧妙的方法是有兩個頂級對象,並使用with助手在第一模板

Handlebars.compile(templateFile)({model:modelObj,other:OtherObj}) 

和模板

<div class="device"> 
    {{#with model}} 
    This is my device default name: {{ defaultName }} <br/> 
    This is my device current state: {{ currentState }} <br/> 
    This is my device last state: {{ lastState }} 
    {{/with}} 
    {{#with other}} 
    {{> options}} 
    {{/with}} 
</div> 
+0

完全是我在找的東西。謝謝! – ChrisJ

相關問題