我试图在我的 Ember(ember-data) 模型中放置一个属性,该属性只是要包含在通用表组件中的属性数组。起初我只是将它添加到我的模型中:
tableColumns: function() {
return ['age', 'gender', 'whatever'];
}.property()
但是现在我发现自己跳过了子组件中的圈子,以便轻松地遍历这些属性并为每个模型实例调用它们的值。
由于列会随每个模型而变化,我认为这是一个很好的解决方案。有没有更好的办法?
特别是当对于每一行(模型实例)我只想说一些不合理的东西时,比如下面的假想片段。
{{#each col in tableColumns}}
<td>{{model.col}}</td>
{{/each}}
我试图保持无控制器并保持我的组件通用。
编辑:
现在在行组件中我正在这样做,然后遍历 hbs 中的“cols”。但这感觉不对,我正在进入异步部分(某些列需要进行外部调用),我认为这会导致一些问题,所以我想找到更好的方法。
this.get('model').tableColumns().forEach(function(cell){
cols.push(that.get('model._data.' + cell));
});