creating records of self referring model causing error

橙三吉。 提交于 2019-12-11 11:28:51

问题


I have self referring model defined as :

        App.Answer = DS.Model.extend({
            name: DS.attr('string'),
            parentAnswer: DS.belongsTo('answer'),
            childAnswers: DS.hasMany('answer')
        });

I am not sure how to define the inverse property to get things working. Here is a jsbin of what I have : http://jsbin.com/oKezUkaz/1/

If we add a group(pressing "Add group" button) we get error in the console saying:

Assertion failed: You defined the 'childAnswers' relationship on App.Answer, but multiple possible inverse relationships of type App.Answer were found on App.Answer. Look at http://emberjs.com/guides/models/defining-models/#toc_explicit-inverses for how to explicitly specify inverses

回答1:


In this case you need to define inverse on both, or it gets stuck in a loop going down one of the path's. Ember Data's documentation is lacking on this, but should be worked on after ED gets to a solid 1.0.

App.Answer = DS.Model.extend({
  name: DS.attr('string'),
  parentAnswer: DS.belongsTo('answer', {inverse: 'childAnswers'}),
  childAnswers: DS.hasMany('answer', {inverse: 'parentAnswer'})
});

http://jsbin.com/oKezUkaz/5/edit




回答2:


Your model must be defined like this

 App.Parent = DS.Model.extend({
        answer: DS.belongsTo('parant')
      });
      App.Child = DS.Model.extend({
        answer: DS.hasMany('child')
      });

        App.Answer = DS.Model.extend({
            name: DS.attr('string'),
          parent: DS.belongsTo('answer'),
            child: DS.belongsTo('answer')
        });

I can't understand what you wanna to achive but i this this is the proper way.



来源:https://stackoverflow.com/questions/21269624/creating-records-of-self-referring-model-causing-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!