Ember model checking isNew vs id == null

I find both isNew and dirtyType to be very unintuitive. Check this out

  1. create a new record => isNew == true and dirtyType == ‘created’
  2. change the record (record.set(‘attr’, ‘something’)) => same as above
  3. rollback the record => isNew == false and dirtyType == ‘deleted’

So, when I have a new record, rollback to its original state, this record is suddenly deleted and no long new? At any rate, what is the best way to check if the record is still new? is It .get(‘id’) == null?

Yes, it is correct behavior, because a new model should be saved when making rollback, in otherwise the model is removed from the application store. Why do you need to use rollback for unsaved model, what is the point?

So in the following scenario, what should I do?

  1. bring up the form, i.e. create a new record (isNew == true)
  2. user edits the form (isNew == false, dirtyType == ‘edited’)
  3. user hits discard (rollback()), and work on the form again…
  4. user hits discard again (rollback())
  5. finally he decides to hit the dimiss form button, (now my ember app just errors out since if i do .deleteRecord() on the new/dirty model, it will raise deleteRecord in saved.deleted state.

I understand perhaps this above scenario sounds like me picking on the framework. But this happened when I was demoing to my customer. Fundamentally, when you have a new record to start with, if you rollback the changes, you should revert back to the pristine, new state. Not a saved and deleted state. In addition, this saved deleted state makes even less sense when you consider a pristine state usually means it doesn’t valdiate yet, so how can you possibly be in a “saved” state, unless one thinks saved state can be reached when the record is invalid.

By the way, has anyone covered the state transition diagram in some posts?

One of the possible solutions is to create a new model every time when user hits discard changes:

model.rollback();
model = this.store.createRecord('...');

and just make rollback to dismiss

model.rollback();

Because model.deleteRecord() and model.rollback() has the same effect for the newly created record. Also you can try this demo http://jsbin.com/uzizip/6