getoutreach / epf

A framework for keeping your Ember.js apps in sync.
http://epf.io
MIT License
369 stars 33 forks source link

Embedded new record get persisted #105

Closed miguelcobain closed 10 years ago

miguelcobain commented 10 years ago

I have a scenario were I create a record and add it to a hasMany embedded relationship.

When I flush the session, these new models I created get persisted (by issuing a POST to the child model url) to the server. It makes sense, since they are indeed new models. However, since they are embedded, they should be persisted along with its owner and not individually.

What I expected was to issue a single POST/PUT to the parent model URL, and this record was serialized along with the parent's JSON.

Is this a bug or something I'm not seeing clearly?

Thank you!

jasonkriss commented 10 years ago

can you post some code? What's your model definition look like? and how are you specifying that the relationship is embedded?

miguelcobain commented 10 years ago

Ok. My main model:

App.Order = Ep.Model.extend({

  symptoms : hasMany('symptom'),
  causes : hasMany('cause'),
  consequences : hasMany('consequence_order'),

  // lots of irrelevant stuff...
});
App.Enum = Ep.Model.extend({
  // Properties
  name : attr('string')
});

App.Consequence = App.Enum.extend({
  systems : hasMany('system'),
  unit : belongsTo('unit'),
  dimension : belongsTo('dimension')
});

App.ConsequenceOrder = Ep.Model.extend({
  consequence : belongsTo('consequence'),
  value : attr('string')
});

And my Order serializer:

App.OrderSerializer = Ep.RestSerializer.extend({
  properties : {
    consequences : {
      embedded : 'always'
    }
  }
});

What I'm doing here is to create an intermediate model (called ConsequenceOrder) to hold values for the relationship. In this case I have a certain set of consequences, but when I assign a consequence to an order, I must provide a value. In the relational model this is a join table with associated values. Example: Order number 1 had a consequence of 10 buildings affected. "Order number 1" -> Order model "10 buildings affected" -> ConsequenceOrder model -> "buildings affected" -> Consequence model

This is a common pattern, but you can just think of it as a chained model relationship hasMany -> belongsTo.

Now, what happens is that when I create a new ConsequenceOrder, add it to an existing Order and flush the session, two requests are issued:

  1. PUT request with the correct ConsequenceOrder models under the consequences property
  2. POST request to consequence_orders url to create a new ConsequenceOrder

I think this happens because epf finds all the new models and processes them equally, regardless of whether they are embedded or not.

I hope I made myself clear. Thank you!

ghempton commented 10 years ago

This shouldn't be the case. We actually have this exact same relationship setup in one of our apps and a single request is issued. I have also significantly changed some of the internals since this issue was opened. I am going to close this issue unless we can get a failing test.