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

Javascript 错误:删除记录模型余烬时尝试处理事件'didSetProperty',javascript,model-view-controller,ember.js,jsbin,Javascript,Model View Controller,Ember.js,Jsbin,我在应用程序中删除记录时遇到问题,我知道发生这种情况的原因,但我无法找出问题所在 我复制了这个案例 我添加发票,然后添加更多交易 问题发生在我删除发票后,这是错误消息 未捕获错误:断言失败:错误:试图在状态为root.deleted.saved时处理事件didSetPropertyon。使用{name:transactionsAmounts,oldValue:NaN,originalValue:undefined,value:0}调用。 基本上,删除发票时存在错误模型{transactionsA

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

我复制了这个案例

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

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

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

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

TransactionMounts是任何单个事务的总和,在模型中创建

  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'),
在删除发票时,TransactionMount未被删除,我如何才能做到这一点,以删除发票模式(有许多交易)而不出现错误?

更新: 这应该在灰烬数据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);
    },

更新: 这应该在灰烬数据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);
    },

我不知道isDeleted状态,对ember来说非常新,非常好的解释,代码和jsBin工作示例您可能还想使用
destroyRecord()
而不是
deleteRecord()
,因此它将被持久保存到服务器API(如果有的话)。这是我在回答中提到的GitHub上的bug问题:我在评论中读到,这是一个通过('isDeleted')筛选项目的解决方法,以避免bug,在我的项目中,我使用的是ember data 1.0.0-beta.10,这是同一个问题,您的代码也在我的应用程序中修复了它,数据版本我不知道isDeleted状态,ember非常新,非常好的解释,代码和jsBin工作示例您可能还想使用
destroyRecord()
而不是
deleteRecord()
,因此它将被持久化到服务器API(如果有)。这是我在回答中提到的GitHub上的bug问题:我在评论中读到,这是一个通过('isDeleted')过滤项目的解决方法,以避免bug,在我的项目中,我使用的是ember data 1.0.0-beta.10,这是同一个问题,您的代码也在我的应用程序中修复了该数据版本