这种情况:
{{#each array itemController="testController"}}
我想在 itemController
中使用索引属性:
App.TestController = Ember.ObjectController.extend({
// Is there any way to get index in this controller
})
或我可以以#each
的方式发送属性:
{{#each array itemController="testController" property=index}}
谢谢
================================================================================
更新:
我确实使用愚蠢的方法来解决此问题
var _temp_index = 0;
App.TestController = Ember.ObjectController.extend({
myIndex: null,
init: function(){
this._super();
this.set('myIndex', _temp_index);
_temp_index++;
}
})
期待更好的解决方案!
您可以使用以下事实:每个事实都会引用其ContentIndex。您可以在车把上做类似的事情:
<ul>
{{#each item in model itemController="test" itemViewClass="App.ItemView"}}
<li>{{item.content}} {{controller.index}}</li>
{{/each}}
</ul>
,然后创建一个设置控制器索引属性的项目视图:
App.ItemView = Ember.View.extend({
didInsertElement: function() {
this.get('controller').set('index',this.get('contentIndex'));
}
});
这是它起作用的小提琴。
http://emberjs.jsbin.com/ecuciqo/2/edit
以下是您无需itemViewClass
的方法。
在您的itemController
中:
export default Em.ObjectController.extend({ // I'm using ES6 syntax but you don't have to
index: function() {
var arrangedContent = this.get('target.content');
var item = this.get('content');
return arrangedContent.indexOf(item) + 1;
}.property('target.content.@each.score'), // Change score to whatever property things are sorted by in the parent controller
});
在您的模板中:
{{#each arrangedContent}}
Index: {{index}}
{{/each}}
使用需求属性以获取arraycontroller
this.get('Model')获取CurrentModel
使用indexof()获取索引
您无需移动逻辑即可以这种方式查看
-
在路由中的数组中的每个模型中添加索引属性
App.SomeParentRoute = Ember.Route.extend setupController: (controller, model) -> model = model.map (item, index) -> item.set 'index', index controller.set 'model', model
-
使用数组控制器存储所有将使用ItemController
的数据App.SomeParentController = Ember.ArrayController.extend itemController: 'someChild'
-
现在,您在每个itemController
中都有someChild
index
属性App.SomeChildController = Ember.ObjectController.extend init: -> console.log @get('index')