0
votes

I'm using Ember 1.4 with EmberData beta7. My Ember-Data models are as shown below:

 /** Ember Data Models **/
 var attr = DS.attr;
 var belongsTo = DS.belongsTo;
 var hasMany = DS.hasMany;

 App.StratLeg = DS.Model.extend({
   legName: attr(),
   quantity: attr(),
   strat: belongsTo('strat')
 });

 App.Strat = DS.Model.extend({
   stratName: attr(),
   stratLegs: hasMany('stratLeg', {async:true})
 });

I created a parent record by doing this in the StratsController:

 var nwParent = 
   {id: "parentId",
    stratName: "pName", 
   };

 var newStrat = this.store.createRecord ('strat', nwParent);

Then transition to the StratsStratController for this newly created record and did the following to create 2 children records and attach those records to the parent:

 var nwChild1 =
 {
  id: "childId1",
  legName: "cName1",
  quantity: '1',
  strat: "parentId"
 };


 var nwChild2 =
 {
  id: "childId2",
  legName: "cName2",
  quantity: '5',
  strat: "parentId"
 };
 var newLeg1 = this.store.createRecord ('stratLeg', nwChild1);
 var newLeg2 = this.store.createRecord ('stratLeg', nwChild2);
 var thisStratLegs = this.get('stratLegs');
 thisStratLegs.pushObject(newLeg1);
 thisStratLegs.pushObject(newLeg2);

If I execute the 'save' statement shown below, Ember sends a message to the server with just the parent record (but parent record sent does not contain the 'strat' attribute); Ember doesn't send the attached children records automatically.

 this.get('model').save();

What's the proper way of persisting both the new parent and new children to the server?

1

1 Answers

0
votes

I believe the 'default' way is to call save() on each of the children manually.

However, this sounds like a good use case for embedded records (assuming you're using a rails backend). Your server would return stratLeg model JSON in an array inside of a strat model's JSON.

If you can't do embedded records, a last resort could be trying to batching up the children's save() calls with something like this.