reduce()
を使って計算したと思ったが、それぞれのレコードで複数のプロパティをロールバックする必要があるので、オブジェクトを返すたびに私が持っている問題は、previousValue
がEmberオブジェクトであり、普通のオブジェクトを返すので、最初のループで正常に動作しますが、2回目にはa
はもはやEmberオブジェクトではなく、エラーが発生しますa.get is not a function
と言っています。サンプルコード:Ember.js:モデルレコードを1つのレコードに集計する
/*
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
から複数のレコードを1つのレコードにロールアップするより良い方法はありますか?あるいは、単純にモデルレコードを平坦なオブジェクトにフラット化するか、代わりにreduce()
のEmberオブジェクトを返すだけですか?
編集:はreturn Ember.Object.create({...})
が仕事をしてやって、私はまだ、この目標を達成するための最良の方法である、またはかどうかにいくつかの意見をしたいと思いエンバーは、この操作を行うと、もしそうなら、彼らはしている場合だろうな機能を提供する場合reduce
よりも優れています。 this.get('model')
を想定し
emberデータを使用していますか? –
@selvaraj:はい私は – redOctober13
ですこのコードはどこですか? – locks