繁体   English   中英

错误:删除记录模型余烬时尝试处理事件`didSetProperty`

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

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

我在这里转载了此案,

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

我删除发票后出现问题,这是错误消息

未捕获的错误:断言失败:错误:试图处理事件didSetProperty上,而在状态root.deleted.saved。 以{名称:transactionAmounts,oldValue:NaN,originalValue:未定义,值:0}进行调用。

基本上,删除发票时会出现错误模型{transactionsAmount}

transactionAmounts是任何单个交易的总和,在此处的模型中创建

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

在删除发票时,不会删除transactionAmount,为了使发票模式(具有hasMany交易)删除而不发生错误,我该如何进行呢?

更新:

这应该在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。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM