简体   繁体   English

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

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

I have an issue when i deleteRecord in my app and i know the reason why this is happening but i can not figure the issue out 我在我的应用程序中删除DeleteRecord时遇到问题,我知道发生这种情况的原因,但我无法解决问题

I have reproduced the case here , 我在这里转载了此案,

I add an Invoice and then i add more transactions. 我添加发票,然后添加更多交易。

The problem happens after i have removed the invoice, this is the error message 我删除发票后出现问题,这是错误消息

Uncaught Error: Assertion Failed: Error: Attempted to handle event didSetProperty on while in state root.deleted.saved. 未捕获的错误:断言失败:错误:试图处理事件didSetProperty上,而在状态root.deleted.saved。 Called with {name: transactionsAmounts, oldValue: NaN, originalValue: undefined, value: 0}. 以{名称:transactionAmounts,oldValue:NaN,originalValue:未定义,值:0}进行调用。

Basically there is an error model {transactionsAmount} on removing the Invoice 基本上,删除发票时会出现错误模型{transactionsAmount}

The transactionAmounts is the sum of any single of transaction and is created here in the models 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'),

At the moment of removing the invoice the transactionAmount is not deleted, how i can make this happening in order to remove the Invoice mode (which hasMany transaction) and not get the error? 在删除发票时,不会删除transactionAmount,为了使发票模式(具有hasMany交易)删除而不发生错误,我该如何进行呢?

Update: 更新:

This should be fixed in Ember Data beta 16 . 这应该在Ember Data beta 16中修复。


Original answer: 原始答案:

Because of bug introduced in Ember Data beta 14 deleted models are still present in collections, so you have to make sure objects you are using aren't deleted. 由于Ember Data beta 14中引入的错误,集合中仍然存在删除的模型,因此您必须确保所使用的对象没有被删除。 This code fixed it for me: 这段代码为我修复了它:

Invoice model: 发票型号:

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 action in controller: 删除控制器中的操作:

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. 的jsfiddle。

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

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