Error: Attempted to handle event `didSetProperty` when Deleting record model ember

Error: Attempted to handle event `didSetProperty` when Deleting record model ember

我在我的应用程序中删除记录时遇到问题,我知道发生这种情况的原因,但我无法解决问题

案例我已复现here,

我添加发票,然后添加更多交易。

问题发生在我删除发票后,这是错误消息

未捕获错误:断言失败:错误:试图在状态 root.deleted.saved 中处理事件 didSetProperty。使用 {name: transactionsAmounts, oldValue: NaN, originalValue: undefined, value: 0} 调用。

基本上在删除发票时存在错误模型 {transactionsAmount}

交易金额是任何单笔交易的总和,并在模型中创建

  transactionsAmounts: DS.attr('string'),
  setTransactionAmount : function(){
    if(this.get("transactions.length")>0){
      this.get("transactions").then(function(transactions){
        var sum=0;
        transactions.forEach(function(transaction){
           sum+=transaction.get("total");
        });
        this.set("transactionsAmounts",sum);
      }.bind(this));
    }
  }.observes('transactions.length', 'transactions.@each.total'),

在删除发票的那一刻,交易金额没有被删除,我怎样才能做到这一点才能删除发票模式(有很多交易)并且不会出现错误?

更新:

这应该在 Ember Data beta 16.

中修复

原回答:

由于 Ember Data beta 14 中引入的错误,删除的模型仍然存在于集合中,因此您必须确保您正在使用的对象未被删除。此代码为我修复了它:

发票型号:

App.Invoice = DS.Model.extend({
  title         : DS.attr('string'),
  transactions  : DS.hasMany('transaction', { async:true}),
  transactionsAmounts: DS.attr('string'),
  setTransactionAmount : function(){
    if(!this.get('isDeleted') && this.get("transactions.length") > 0){
      this.get("transactions").then(function(transactions){
        var sum=0;
        transactions.forEach(function(transaction){
          if(!transaction.get('isDeleted'))
          {
            sum += transaction.get("total");
          }
        });
        if(!this.get('isDeleted'))
        {
          this.set("transactionsAmounts",sum);
        }
      }.bind(this));
    }
  }.observes('transactions.length', 'transactions.@each.total'),
});

移除控制器中的动作:

remove: function() {
      var transactions = this.get('model.transactions'),
          list = transactions.toArray();
      list.forEach(function(transaction) {
        if (!transaction.get('isDeleted'))
        {
          transaction.deleteRecord();
          transactions.removeObject();
        }
      });
      var model = this.get('model');
      if(!model.get('isDeleted'))
      {
        this.get('model').deleteRecord();
      }
      // and then go to the fatturas route
      this.transitionToRoute('fatturas');
      // set deleteMode back to false
      this.set('deleteMode', false);
    },

JSFiddle.