问题
I am trying to achieve this Department -HasMany->Contacts.
Have no clue Department gets saved but Contacts doesnt get Saved
you can refer this section in jsbin
App.NewcontactController = Ember.ObjectController.extend({
needs: ['department'],
save: function () {
var department = this.get('controllers.department').get('model');
var newContact = App.Contact.create({
name: this.get('name'),
department: department
});
department.get('contacts').addObject(newContact);
console.log(department);
console.log(newContact);
department.get('contacts').save();
department.save();
console.log('---saved contact---');
this.transitionTo('contact',newContact);
}
});
Firebug LocalStorage
Contact-1 "{"id":"1","department_id":"1"}"
Department-1 "{"id":"1","name":"A","contact_ids":[]}" //NO ids :(
*My Jsbin **
UPDATED Ember-Model with Ember-Model-LocalStorage
回答1:
Saving a parent doesn't save children. You are responsible for saving the children yourself. There is a save
method on the hasMany relationship to make this a little easier.
来源:https://stackoverflow.com/questions/18686562/ember-model-hasmany-does-not-save-model-emberjs