2015-09-24 25 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

相关问题