如何在ember js中以一对一的关系添加父对象到子对象 [英] How to add parent object to child in a one-to-may relationship in 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 sometimeschild-object
. Same goes forparent
. findRecord
returns aPromiseObject
. 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屋!