ember-data-change-tracker icon indicating copy to clipboard operation
ember-data-change-tracker copied to clipboard

except not filtering change

Open BryanHunt opened this issue 8 years ago • 4 comments

I updated my model to include an except on the "updatedOn" field so I could set it when the model is saved. Now, when I save the model, I get into an infinite loop with updatedOn flagged as changed.

Here is the model config: changeTracker: { trackHasMany: true, auto: true, enableIsDirty: true, except: ['updatedOn'] }

Here is the save code:

export default Route.extend({
  autosave: task(function * () {
    yield timeout(3000);
    this.set('model.updatedOn', moment());
    this.get('model').save();
  }).restartable(),

  model(params) {
    return this.store.findRecord('task', params.taskId);
  },

  afterModel(model) {
    this.set('model', model);
  },

  modelChanged: observer('model.isDirty', function() {
    if(this.get('model.isDirty')) {
      window.console.log(this.get('model').changed());
      this.get('autosave').perform();
    }
  })
});

And the output from the console:

{deliverables: Array(2), subscribers: true}
{deliverables: Array(2), updatedOn: Array(2), subscribers: true}
{subscribers: true}
{updatedOn: Array(2)}
{}
{updatedOn: Array(2)}
{updatedOn: Array(2)}

BryanHunt avatar Sep 06 '17 14:09 BryanHunt

Think about this carefully. Think really carefully about what you are doing and you will see why there is an infinite loop.

Hint: Your autosave concept is alittle bit aggressive

danielspaniel avatar Sep 06 '17 23:09 danielspaniel

I don't think my autosave is aggressive at all. When isDirty goes active, I request a save. The save yields for 3 seconds. If there is another change, the save will be pre-empted and a new one scheduled. It's not until there is no change for three seconds that the model is actually saved. It is at that time that I set updatedOn.

The problem is that setting updatedOn is causing isDirty to go active even though updatedOn is in the list of "except". I assert that either except is not working correctly, or my understanding of except is wrong.

BryanHunt avatar Sep 07 '17 15:09 BryanHunt

Can you show me that model definition?
Nevermind. What is happening is that the isDirty computed property does not consider the except list. The list of except was only implemented for tracking and rolling back attributes/relationship when you call rollback() isDirty is supposed to just say .. "hey ... your model is dirty" and is not necessarily used by people who use the startTrack/rollback concept.

Therefore isDirty tracks everything so you can tell if the model is changed and can do things if you like with that info. Make sense?

Not sure if it makes sense to change that??

Got any thoughts on this.

danielspaniel avatar Sep 07 '17 16:09 danielspaniel

@BryanHunt , I am actually starting to like your autosave method. It is pretty interesting.

Do you have any thoughts on wether the except should refer to the isDirty?

I am wondering why this issue even occurs because by the time autosave is called the model will be isDirty (true) so setting updatedOn should not affect anything at all. So that was puzzling.

Anyway .. let me know what you think, or what I should I look into more, or close the issue. If you want to get on slack and talk about it, that is fine by me as well.

danielspaniel avatar Jan 26 '18 14:01 danielspaniel