Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make the parent dirty when I change a hasMany/belongsTo relationship in Ember-Data?

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.

like image 925
miguelcobain Avatar asked Nov 15 '13 10:11

miguelcobain


People also ask

Which of the following Cannot be a reflexive relationship in Ember?

<br> `therefore` 512 cannot be the number of reflexive relations defined on a set A.

What are polymorphic models in Ember?

Polymorphism. Polymorphism is a powerful concept which allows a developer to abstract common functionality into a base class. Consider the following example: a user with multiple payment methods. They could have a linked PayPal account, and a couple credit cards on file.

What are adapters in Ember?

In Ember Data, an Adapter determines how data is persisted to a backend data store. Things such as the backend host, URL format and headers used to talk to a REST API can all be configured in an adapter. Ember Data's default Adapter has some built-in assumptions about how a REST API should look.

What is store in Ember?

One way to think about the store is as a cache of all of the records that have been loaded by your application. If a route or a controller in your app asks for a record, the store can return it immediately if it is in the cache.


2 Answers

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.

like image 192
spDuchamp Avatar answered Oct 21 '22 16:10

spDuchamp


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

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

like image 40
AdamLC Avatar answered Oct 21 '22 16:10

AdamLC