简体   繁体   中英

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

Ember Data: 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.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 :

models/post.js

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

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