如何在完成每件产品后解决最终总和?
目前return sum;
正在返回0
。我可以理解,因为this.get('product')
是async
。
我需要使用RSVP
吗?还是其他方法?
subtotalInCents: function () {
var sum = 0;
this.get('items').forEach(function(item) {
item.get('product').then(function(product){
sum += item.get('count') * product.get('amountInCents');
console.log(sum);
});
});
return sum;
}.property('items.@each.count'),
答案 0 :(得分:0)
请改用观察者。
subtotalInCents: 0,
didItemsChange: function () {
var that = this;
var items = this.get('items');
var itemCount = items.get('length');
var sum = 0;
items.forEach(function(item) {
item.get('product').then(function(product){
sum += item.get('count') * product.get('amountInCents');
itemCount--;
if (itemCount === 0) {
that.set('subtotalInCents', sum);
};
});
});
}.observes('items.@each.count')