[英]Backbone - Passing index into View when iterating over a Collection?
I'm trying to render a View which needs to know the index position of the Model passed in inside its Collection: 我正在尝试渲染一个View,该View需要知道在其Collection内部传递的Model的索引位置:
Parent View 父视图
collection.each(function($_v, $_k) {
(new view({model: $_v, index: $_k})).render();
});
Child View 儿童观
className: function() {
0 === this.index % 2 ? 'colored' : '';
}
However it appears that this.index
can't be set this way and become undefuined
. 然而似乎
this.index
不能设置这种方式,并成为undefuined
。 What is the proper to make this work? 使这项工作合适的是什么?
I think it should be: 我认为应该是:
className: function() {
0 === this.options.index % 2 ? 'colored' : '';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.