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

Asked By: Anonymous

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

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. Called with {name: transactionsAmounts, oldValue: NaN, originalValue: undefined, value: 0}.

Basically there is an error model {transactionsAmount} on removing the Invoice

The transactionAmounts is the sum of any single of transaction and is created here in the models

  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', '[email protected]'),

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?


Solution

Answered By: Anonymous

Update:

This should be fixed in 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. 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', '[email protected]'),
});

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.

techinplanet staff

techinplanet staff


Windows 10 Kaufen Windows 10 Pro Office 2019 Kaufen Office 365 Lizenz Windows 10 Home Lizenz Office 2019 Home Business Kaufen windows office 365 satın al follower kaufen instagram follower kaufen porno