It is odd how models with multiple hasMany are being saved. I’ve spend good amount of time trying to figure it out
let’s say I have following model:
window.App.Basket = DS.Model.extend({
fruits: DS.hasMany('fruit', {
async: true,
}),
vegetables: DS.hasMany('vegetable', {
async: true,
}),
});
And then I want to add another alement to fruits and save it:
window.App.FruitsRoute = window.Ember.Route.extend({
actions: {
addFruit: function (model) {
var basket = this.modelFor('basket');
basket.get('fruits').then(function (fruits) {
fruits.pushObject(model);
basket.save();
});
},
},
});
Vegetables array posed to server will be empty! This behaviour seems very odd to me.
I’ve managed to solve it by adding custom save function (forcing model to load all relations) to basket model:
save_fixed: function () {
// Forcing 'fruits' relation load before saving
this.get('fruits').then(function () {
// Forcing 'vegetables' relation load before saving
return this.get('vegetables'));
}).then(function () {
// Saving parent only after all hasMany items has been loaded
this.save();
}.bind(this));
},
Ember Data version that I am using is 1.0.0-beta.7+canary.e3b896bc
and Ember version 1.4.0
This issue is very similar to these issues on StackOverflow:
Is this a bug, or, if not, how it is supposed to work?