考え方私はAndrew Hubbsの提案がどのように私を助けたかを分かります。私は、私のアイテムテンプレートで親モデルのプロパティをインラインで表示しようとしていました。私は、MarionetteのtemplateHelpersプロパティを使用して、これをAndrewの提案の1つと組み合わせて使用しました。
<h1>Page {{name}}</h1>
<h6>Children</h6>
<ul></ul>
例の項目テンプレート - myItemTemplate:
{{name}} is child of: {{getParentName}}
再生回数:
App.module('App.View', function(View){
View.MyItemView = Marionette.ItemView.extend({
initialize: function(options) {
this.parentModel = options.parentModel;
},
template: myItemTemplate,
tagName: 'li',
templateHelpers: function() {
var view = this;
return {
// Called by item template, returns parent model 'name' property.
getParentName: function() {
return view.parentModel.get('name');
}
};
}
});
View.MyView = Marionette.CompositeView.extend({
template: myViewTemplate,
itemView: View.MyItemView,
itemViewContainer: 'ul',
itemViewOptions: function() {
return { parentModel: this.model };
}
});
});
- MYVIEWテンプレート
例コンポジットテンプレート:
私は例の簡単なを維持しようとしました
これがどのように実装されるかの例:
// example of how implementation
// parent model has an attribute called 'children', which is a collection of models
var children = parent.get('children');
var view = new App.View.MyView({ model: parent, collection: children });
App.mainRegion.show(view);
ありがとう。あなたは、私の質問のポイント、つまり子供の各インスタンスに親モデルをどうやって取得するのかを明確に理解しました。 3つのオプションすべてが便利です。 –
これは素晴らしい説明でした。ありがとうございました! – alettieri
Marionette 2の時点では、 '' itemViewOptions''は '' childViewOptions''に名前が変更されました。(CollectionView/CompositeView内の '' itemView''への他の参照と一緒に) – pimlottc