I have a problem similar to this question , except that the answer does not seem to work. I have a form in which the user creates a container module with a variable number of submodels. When the form is submitted, I have to save the container, submodels and make sure that hasMany relationships are saved. My code (using Ember-Cli):
container :
var Container= DS.Model.extend({ name: DS.attr('string'), submodels: DS.hasMany('submodel'), lastModified: DS.attr('date') }); export default Container;
submodel:
var Submodel= DS.Model.extend({ char: DS.belongsTo('container'), name: DS.attr('string'), desc: DS.attr('string'), priority: DS.attr('number'), effort: DS.attr('number'), time: DS.attr('number') }); export default Submodel;
ContainersNewRoute:
export default Ember.Route.extend({ model: function() { return this.store.createRecord('container', { ... }); } });
ContainersNewController:
export default Ember.ObjectController.extend({ actions: { addSubmodel: function() { var submodels= this.get('model.submodels'); submodels.addObject(this.store.createRecord('submodel', { ... })); }, saveNewContainer: function () { var container = this.get('model'); container.save().then(function(){ var promises = Ember.A(); container.get('submodels').forEach(function(item){ promises.push(item.save); console.log(item); }); Ember.RSVP.Promise.all(promises).then(function(resolvedPromises){ alert('all saved'); }); }); this.transitionToRoute(...); } } });
Ember Data itself works fine, moving on to presenting the created container with the listed submodels. Refresh the page and the submodels will disappear from the container view.
I tried several options, for example using pushObject, and not addObject from the response to a stack overflow. I also tried using the Ember.RSVP callback to run container.save () a second time after saving the submodels.
After some additional testing, I found that submodels are not saved at all.
Is there any reasonable way to save 1) a container 2) submodels 3) hasMany / belongs to relationships with each other?
Or does it somehow need to be broken down into discrete steps when I save the container, save the submodels, click the submodels in the container to get the hasMany relationships and recreate the container, and finally make the submodels belong to the container and save the submodels again?