如何在ember js中以一对一的关系添加父对象到子对象 [英] How to add parent object to child in a one-to-may relationship in ember js

查看:140
本文介绍了如何在ember js中以一对一的关系添加父对象到子对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个模型在ember.js与一对一的关系。现在我想创建一个新的子对象,需要分配一个关联的父对象。我不知道如何做到这一点。我的模型使用以下代码定义:



父对象模型

 从'ember-data'导入DS; 

export default DS.Model.extend({
name:DS.attr('string'),
children:DS.hasMany('child')
});

子对象模型

 从ember-data导入DS; 

export default DS.Model.extend({
name:DS.attr('string'),
parent:DS.belongsTo('parent')
});

model() -method of路由创建一个子对象。

  var child = this.store.createRecord('child'); 

然后查询父对象。

  var parent = this.findRecord('parent',2); 

然后我试图将它们绑定在一起,但是我尝试失败。例如:

  parent.get('child')。addObject(child)//或
parent.get ('child')。pushObject(child)//或
child.set('parent',parent)

这些事情导致 child 中没有任何东西 parent 中没有任何内容。我也做了一些尝试与异步承诺解决,但没有成功。如果有人可以发表一个例子如何管理这将是很棒的。

解决方案

基本上这是按照你的期望工作的。但是你有一些小的错误:




  • 有时您使用 child code>子对象。 c code code code code返回一个 PromiseObject 。你可能希望等待承诺解决。

  • 它不是 addObject



您也可以从双方做到。设置父项:

  this.store.findRecord('parent-object',2).then(parent => {
const child = this.store.createRecord('child-object');
child.set('parent',parent);
});

或者您将孩子添加到孩子:

  this.store.findRecord('parent-object',2).then(parent => {
const child = this.store.createRecord('child-对象');
parent.get('children')。pushObject(child);
});

也许查看这个旋转


I have two models in ember.js with a one-to-may relationship. Now I want to create a new child object and need to assign an associated parent object. I can't figure out how to do this. My models are defined with the following code:

model of parent-object

import DS from 'ember-data';

export default DS.Model.extend({
    name: DS.attr('string'),
    children: DS.hasMany('child')
});

model of child-object

import DS from 'ember-data';

export default DS.Model.extend({
    name: DS.attr('string'),
    parent: DS.belongsTo('parent')
});

In the model()-method of the route create a child object.

var child = this.store.createRecord('child');

and then query for the parent-object.

var parent = this.findRecord('parent', 2);

Then I tried to bind them together but anythig I tried failed. For example:

parent.get('child').addObject(child) // or
parent.get('child').pushObject(child) // or
child.set('parent', parent)

These things result in parent not having anything in child and child not having anything in parent. I also did some attempts with async promise resolving but with no success. It would be great if someone could post an example how to manage this.

解决方案

Basically this works as you expect. But you have a few minor errors:

  • Sometimes you use child and sometimes child-object. Same goes for parent.
  • findRecord returns a PromiseObject. You probably want to wait for the promise to resolve.
  • Its not addObject.

Also you can do it from both sides. Either set the parent:

this.store.findRecord('parent-object', 2).then(parent => {
  const child = this.store.createRecord('child-object');
  child.set('parent', parent);
});

Or you add the child to the children:

this.store.findRecord('parent-object', 2).then(parent => {
  const child = this.store.createRecord('child-object');
  parent.get('children').pushObject(child);
});

Maybe check out this twiddle.

这篇关于如何在ember js中以一对一的关系添加父对象到子对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆