javascript Marionette.CompositeView,如何将参数传递给 Marionette.ItemView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11273115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Marionette.CompositeView, how to pass parameters to Marionette.ItemView
提问by Lorraine Bernard
I would like to access the app.vent from Marionette.ItemView.
我想从 Marionette.ItemView 访问 app.vent。
Maybe an option could be to pass a parameter (app.vent
) to Marionette.ItemView
from Marionette.CompositeView
.
也许一个选项是将参数 ( app.vent
)传递给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.
PS:
我无法从 itemView 访问该应用程序,因为存在循环依赖问题。
app -> view/compositeView -> view/itemView
回答by Derick Bailey
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.
v0.9 添加了一个itemOptions
可以用于此的属性。它可以是对象字面量,也可以是返回对象字面量的函数。
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
key: "value"
此属性返回的所有对都将提供给初始值设定项中的 itemview 选项
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.
此外,如果您需要为构建的每个 itemView 实例运行特定代码,您可以覆盖该buildItemView
方法以为集合中的每个对象提供项目视图的自定义创建。
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
- v0.9 的变更日志
- itemViewOptions 的 CollectionView 文档- 请注意,CompositeView 从 CollectionView 扩展,因此所有 CollectionView 文档也适用于 CompositeView
- buildItemView 注释源代码
回答by DiegoG
Since Marionette v2.0.0, childViewOptionsis used instead of itemViewOptionsto pass parameters to the child view:
由于木偶V2.0.0,childViewOptions代替itemViewOptions将参数传递给该子视图:
var MyCompositeView = Marionette.CompositeView.extend({
childView: MyChildView,
childViewOptions: function(model, index) {
return {
vent: this.options.vent
}
}
});
var MyChildView = Marionette.ItemView.extend({
initialize: function(options) {
// var events = options.vent;
}
});
new MyCompositeView({ vent: app.vent, collection: myCollection});
But to work with events, lets use Marionette.Radioinstead of passing app.ventto the view.
但是要处理事件,让我们使用Marionette.Radio而不是将app.vent传递给视图。