【问题标题】:Error: Attempted to handle event `didSetProperty` when Deleting record model ember错误:删除记录模型余烬时尝试处理事件“didSetProperty”
【发布时间】:2015-01-18 11:01:10
【问题描述】:

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

我已经转载了案例here,

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

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

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

删除 Invoice 时基本上有一个错误模型 {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 交易)而不出现错误?

【问题讨论】:

    标签: javascript model-view-controller ember.js jsbin


    【解决方案1】:

    更新:

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

    【讨论】:

    • 我不知道 isDeleted 状态,对 ember 很新,很好的解释,代码和 jsBin 工作示例
    • 您可能还想使用destroyRecord() 而不是deleteRecord(),以便将其持久化到服务器API(如果有)。这是我在回答中提到的错误在 GitHub 上的问题:github.com/emberjs/data/issues/2666
    • 我在 cmets 中读到,这是通过 ('isDeleted') 过滤项目以避免错误的解决方法,在我的项目中,我使用的是 ember-data 1.0.0-beta.10,它是同样的问题,您的代码也在我的应用程序中使用该数据版本修复了它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-06
    • 2018-01-15
    • 1970-01-01
    • 2015-09-12
    相关资源
    最近更新 更多