简体   繁体   中英

How to create an object in a one to many relationship

I have parents and children. As you'd expect, a Parent 'hasMany' Child objects. I am able to create a new Parent just fine, but I can not get new Child objects to show up on my template. According to the Chrome and Firefox Ember debug tool, it looks like the Child objects are being created in the store correctly. But the new children never appear in the template.

I believe I've followed the Ember docs to the letter, and yet I can not get my code to work.

I've created the following jsbin to demonstrate my situation. http://emberjs.jsbin.com/zodorule

How do I create new many-related objects in Ember?

Ember Data isn't a database, so setting the relationship from one side won't automatically hook it up from the other.

  var child = this.store.createRecord('Child', {name: 'Fred'});
  this.store.find('parent', 1).then(function(parent) {
    child.set('parent', parent);
    parent.get('children').then(function(children){
      children.pushObject(child);
    });
  });

Example: http://jsbin.com/xorov/1/edit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM