模板两款车型在一个视图 - 骨干/木偶 [英] Template two models in one view - Backbone/Marionette
本文介绍了模板两款车型在一个视图 - 骨干/木偶的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在一个视图中使用两种模式,并使用他们两个模板。我与木偶的工作。这是我的观点初始化:
I'm trying to use two models in one view, and template using both of them. I'm working with Marionette. Here is me initialization of the view:
main_app_layout.header.show(new APP.Views.HeaderView({
model: oneModel,
model2 : twoModel}
));
下面是我的看法:
APP.Views.HeaderView = Backbone.Marionette.ItemView.extend({
template : '#view_template',
className: 'container',
initialize: function() {
//This correctly logs the second model
console.log(this.options.model2);
}
});
这是模板:
<script id="view_template" type="text/template">
<p>{{twoModel_label}} {{oneModel_data}}</p>
<p>{{twoModel_label2}} {{oneModel_data2}}</p>
</script>
它使得一切正确使用oneModel数据,但不呈现第二,即使它正确地记录它。我使用的胡子作为我的模板语言。
It renders everything correctly using the oneModel data, but doesn't render the second, even though it logs it correctly. I'm using Mustache as my templating language.
谁能帮助?
推荐答案
您可以覆盖 serializeData code>方法对你的看法,并使其返回这两种模式的数据:
You can override the serializeData
method on your view and have it return both models' data:
APP.Views.HeaderView = Backbone.Marionette.ItemView.extend({
// ...
serializeData: function(){
return {
model1: this.model.toJSON(),
model2: this.options.model2.toJSON()
};
}
});
然后,你的模版是这样的:
Then your template would look like this:
<script id="view_template" type="text/template">
<p>{{model1.label}} {{model1.data}}</p>
<p>{{model2.label}} {{model2.data}}</p>
</script>
这篇关于模板两款车型在一个视图 - 骨干/木偶的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文