Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

except not filtering change #29

Open
BryanHunt opened this issue Sep 6, 2017 · 4 comments
Open

except not filtering change #29

BryanHunt opened this issue Sep 6, 2017 · 4 comments

Comments

@BryanHunt
Copy link

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)}
@danielspaniel
Copy link
Owner

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

@BryanHunt
Copy link
Author

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.

@danielspaniel
Copy link
Owner

danielspaniel commented Sep 7, 2017

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
Copy link
Owner

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants