簡體   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