14
votes

My main problem with Ember Data, right now, is that when I change a relationship (hasMany or belongsTo), the parent doesn't get dirty.

I need this because:

  • I'm depending on the isDirty property to show a save/cancel button
  • hasMany and belongsTo ids are sent with the parent, so the parent in fact IS dirty.

Also, when I rollback the parent, only the belongsTo relationships are reverted. The hasMany models stay the same.

I've found this issue that talks about a dirtyRecordsForHasManyChange hook, but that doesn't seem to exist in Ember Data v1 (v1.0.0-beta.3, which is what I'm using).

How can I accomplish this?

Thanks.

2

2 Answers

11
votes

For a belongsTo I added an observer.

For example in my I have Person with a belongsTo Province. On my form I have an ember select for the provinces. In the Person model I add this observer...

provinceChanged: function() {
  this.send('becomeDirty');
}.observes('province')

I too am depending on isDirty for showing/hiding Save/Cancel buttons, and while that observer does a fine job at marking the record as dirty, if I hit cancel, I do the rollback, but I also need to mark the record clean. I do this in the Person controller on my cancel action.

cancel: function() {
  this.get('model').rollback();
  this.set('isEditing', false);
  this.get('model').adapterWillCommit();
  this.get('model').adapterDidCommit();
}

This all seems to be working quite well.

For hasMany on another project we used a computed property on the controller.

isThisOrChildrenDirty: function() {
  return this.get('isDirty') || this.get('authors').get('isDirty');
}.property('isDirty','[email protected]')

Then instead of checking isDirty we check isThisOrChildrenDirty.

Hope that's helpful.

4
votes
this.get('model').send('becomeDirty');

That should do this trick. Just send becomeDirty to the parent model.