如何在完成每件产品后退还金额?

时间:2015-03-10 10:26:06

标签: ember.js ember-data ember-cli

如何在完成每件产品后解决最终总和?

目前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'),

1 个答案:

答案 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')