Marionette.CompositeView,如何参数传递给Marionette.ItemView [英] Marionette.CompositeView, how to pass parameters to Marionette.ItemView
问题描述
我想从Marionette.ItemView访问app.vent。
也许一个选项,可以从<$传递一个参数( app.vent
)以 Marionette.ItemView
C $ C> Marionette.CompositeView 。
下面我code:
//查看/ compositeView.js
定义([
'木偶',
意见/项'
],功能(木偶,ItemView控件){
VAR的ListView = Marionette.CompositeView.extend({
ItemView控件:ItemView控件
});
});
任何想法?
P.S:结果
因为循环依赖的问题,我无法访问ItemView控件从应用程序。
程序 - &GT;查看/ CompositeView中 - &GT;查看/ ItemView控件
V0.9补充,可用于本的 itemOptions
属性。它可以是对象文本或返回一个对象的函数文本。
Backbone.Marionette.CompositeView.extend({
ItemView控件:MyItemViewType, itemViewOptions:{
一些:选项,
云:这里
}});
所有键:价值是由该属性返回的,在格兰初始化提供给ItemView控件的选项
对
Backbone.Marionette.ItemView.extend({
初始化:功能(选件){
options.some; // =>选项
options.goes; // =>这里
}
});
此外,如果需要运行特定的code为构建每个ItemView控件实例,则可以覆盖 buildItemView
方法来提供项目视图定制创作对于集合中的每个对象
buildItemView:函数(项目,ItemView控件){ //做定制的东西在这里 VAR视图=新ItemView控件({
型号:项目,
//此处添加您自己的选项 }); //更多的自定义code工作过的视图实例 返回视图。
},
有关更多信息,请参见:
- the更改日志V0.9
- the对于itemViewOptions 的文档的CollectionView - 注意,CompositeView中从延伸的CollectionView,因此所有的CollectionView文档的有效期为CompositeView中以及
- the buildItemView注明出处code
I would like to access the app.vent from Marionette.ItemView.
Maybe an option could be to pass a parameter (app.vent
) to Marionette.ItemView
from Marionette.CompositeView
.
Here my code:
// view/compositeView.js
define([
'marionette',
'views/item'
], function (Marionette, itemView) {
var ListView = Marionette.CompositeView.extend({
itemView: itemView
});
});
Any ideas?
P.S.:
I cannot access the app from itemView because there is a problem of circular dependency.
app -> view/compositeView -> view/itemView
v0.9 added an itemOptions
attribute that can be used for this. It can either be an object literal or a function that returns an object literal.
Backbone.Marionette.CompositeView.extend({
itemView: MyItemViewType,
itemViewOptions: {
some: "option",
goes: "here"
}
});
All of the key: "value"
pairs that are returned by this attribute will be supplied to the itemview's options in teh initializer
Backbone.Marionette.ItemView.extend({
initialize: function(options){
options.some; //=> "option"
options.goes; //=> "here"
}
});
Additionally, if you need to run specific code for each itemView instance that is built, you can override the buildItemView
method to provide custom creation of the item view for each object in the collection.
buildItemView: function(item, ItemView){
// do custom stuff here
var view = new ItemView({
model: item,
// add your own options here
});
// more custom code working off the view instance
return view;
},
For more information, see:
- the change log for v0.9
- the CollectionView documentation for itemViewOptions - note that CompositeView extends from CollectionView, so all CollectionView docs are valid for CompositeView as well
- the buildItemView annotated source code
这篇关于Marionette.CompositeView,如何参数传递给Marionette.ItemView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!