0

In my use case I want to invalidate a model when some of its properties change so that the user needs to revalidate before publishing his changes

invalidate: function () {
    this.set('model.valid', false);
}.observes('model.solution', 'model.setup', 'model.tests')

the problem (I think) is the observer fires whenever the model changes, including when it is loaded, which may be a time where the model is valid and all attributes didn't change, but since it fires valid is set to false.

using isDirty was not helpful as the model is then always dirty

Incase my intent isn't obvious, what I have is a model that I want to be invalidated whenever some properties change, saving changes while invalid causes to model to be unpublished, it also required that the model be valid in order to publish it (however not to save it).

currently my workaround is to just validate when publishing but I would prefer if I can do the former.

Amr Draz
  • 2,806
  • 3
  • 20
  • 26

2 Answers2

0

Yes, tricky situation. Any time a model switches all of those properties have changed. In that case, I'd define invalidate on the model itself, that way the only time they change is when that particular model changes.

App.Foo = DS.Model.extend({
  ...
  valid: true,
  invalidate: function () {
    this.set('valid', false);
  }.observes('solution', 'setup', 'tests')
});
Kingpin2k
  • 47,277
  • 10
  • 78
  • 96
0

I'm having the same issue, but I've found some workaround that works in my case.

Please see the link below. There is an example of using "observesBefore" function instead of "observes" How can an observer find out the before and after values of the observed property in Ember.js?

I use observesBefore with arguments to check previous state of the model and the new attribute value being set to find out whether this is the first time the model loaded or changes made by the user.

Community
  • 1
  • 1
Vladislav Lezhnin
  • 837
  • 1
  • 7
  • 17