Ember Data-createRecord in a hasMany relationship

I am using Ember Data beta2 and I have hasMany installed.

When creating a child entry do I need to use pushObject for the parent matching property?

When viewing the documentation, I get the impression that I need to set the parent property of the record correctly and save it.

Here is how I do it:

addPlugin: function() { //get the value var title = this.get('newPluginName'); if (!title.trim()) { return; } var plugin = { name: title, category: this.get('model'), url: '' }; var plugin = this.store.createRecord('plugin', plugin); plugin.save(); //clear the text field this.set('newPluginName', ''); $("#new-plugin").blur(); } 

I see a newly created entry in the Ember inspector in Chrome, it is not dirty, but it is not in the parent listing, and after updating it disappeared.

+1
source share
1 answer

What works for me:

 var child = this.get('store').createRecord('child', { name: 'New Child', parent: parent }; child.save(); parent.get('children').addObject(child); // My backend automatically adds the inverse of relationships when saving // the child, so I don't need to save the parent separately 

I don’t know what addPlugin belongs addPlugin , but if you create children from ChildArrayController you can enable

 needs: ['parent'] 

in your controller. And before you create a child and add it to the parent, you need to call:

 var parent = this.get('controllers.parent'); 
+8
source

Source: https://habr.com/ru/post/970745/


All Articles