简体   繁体   English

Ember.js嵌套模型

[英]Ember.js nested models

I try to make repost with relation on original post. 我尝试与原始帖子建立关系。 I use objects of the same model. 我使用相同模型的对象。

Ember: 2.2.0 灰烬:2.2.0

Ember Data: v2.3.0 灰烬数据:v2.3.0

models/post.js

export default DS.Model.extend({
  text: DS.attr('string'),
  originalPost: DS.belongsTo('post', {async: false})
});

If I create repost with link on original post 如果我使用原始帖子上的链接创建转发

let post = this.store.createRecord('post', {
  text: 'post'
});

let repost = this.store.createRecord('post', {
  text: 'repost',
  originalPost: post
});

I've got cross reference ie: nested post in repost as expected and nested repost in post as I don't expect. 我有交叉引用,即:如预期的那样,在repost中嵌套嵌套,而如我所期望的那样,在post中嵌套嵌套。

repost.get('originalPost') -> post

post.get('originalPost') -> repost ???

Who can explain this behavior and how I can avoid this to create only one directional relation? 谁可以解释这种行为,我又如何避免这种行为而仅创建一个方向关系?

Thanks! 谢谢!

You need to specify the inverse when defining a relation from a model to that same model. 在定义从模型到同一模型的关系时,需要指定反函数 it seems that you don't want an inverse, so you should specify null : 似乎您不希望求逆,因此应指定null

models/post.js 模型/ post.js

export default DS.Model.extend({
  text: DS.attr('string'),
  originalPost: DS.belongsTo('post', {async: false, inverse: null})
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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