【发布时间】:2016-05-13 15:02:26
【问题描述】:
我以为我已经用reduce() 解决了这个问题,但问题是我需要在每条记录上汇总多个属性,所以每次我返回一个对象时,我遇到的问题是@ 987654322@ 是一个 Ember 对象,我返回的是一个普通对象,所以它在第一个循环中运行良好,但第二次通过时,a 不再是一个 Ember 对象,所以我收到一条错误消息 a.get is not a function .示例代码:
/*
filter the model to get only one food category, which is determined by the user selecting a choice that sets the property: theCategory
*/
var foodByCategory = get(this, 'model').filter(function(rec) {
return get(rec, 'category') === theCategory;
});
/*
Now, roll up all the food records to get a total
of all cost, salePrice, and weight
*/
summary = foodByCategory.reduce(function(a,b){
return {
cost: a.get('cost') + b.get('cost'),
salePrice: a.get('salePrice') + b.get('salePrice'),
weight: a.get('weight') + b.get('weight')
};
});
这一切都错了吗?有没有更好的方法将多个记录从model 汇总到一个记录中,还是我只需要先将模型记录展平为普通对象,或者在reduce() 中返回一个 Ember 对象?
编辑: 执行 return Ember.Object.create({...}) 确实有效,但我仍然想了解这是否是实现目标的最佳方式,或者 Ember 是否提供了可以执行此操作的函数,以及是否所以,如果他们比 reduce 更好的话。
【问题讨论】:
-
你在使用 ember 数据吗?
-
@locks: 在我的控制器中
标签: ember.js ember-data