より多くのトランザクションで構成された請求書があります。すべてのトランザクションには、数量と運賃の 2 つの値の乗算から得られる最終結果として合計金額があります。
私は、トランザクションのこれらすべての合計の合計を計算しようとしています
これはUncaught TypeError: Cannot read property 'getEach' of undefinedというエラーです
これが発生する理由を理解しています。値の合計はまだ存在しません (まだ計算されていないため)。
これは関数を持つ私のモデルですtransactionsAmounts
App.Invoice = DS.Model.extend({
title : DS.attr('string'),
transactions : DS.hasMany('transaction', { async:true}),
transactionsAmounts: function() {
var sum = function(s1, s2) { return s1 + s2; };
return this.get('model').getEach('total').reduce(sum);
}.property('model.@each.total'),
});
App.Transaction = DS.Model.extend({
quantity: DS.attr('string'),
fare: DS.attr('string'),
total: DS.attr('string'),
invoice: DS.belongsTo('invoice'),
updateTotal: function() {
// get the reference to the values of fare and quantity
var quantity = this.get('quantity'),
fare = this.get('fare');
// massage them to make sure your stuff is not gonna break
if (isNaN(fare)) { fare = 0; }
if (isNaN(quantity)) { quantity = 0; }
// calculate
var total = fare * quantity;
// set the total
this.set('total', total);
}.observes('quantity', 'fare')
});
これは、すべての合計を計算するために使用した別の関数であり、同じエラーが発生します
transactionsAmounts: function(){
var totals = this.get("total");
return totals.reduce(function(previousValue, total){
return previousValue + totals.get("transactionsAmounts");
}, 0);
}.property("totals.@each.total")
どうすればできますか?