创建自我引用模型的记录导致错误

时间:2014-01-21 21:37:29

标签: javascript ember.js ember-data

我将自我推荐模型定义为:

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

我不确定如何定义逆属性以使事情有效。这是我所拥有的jsbin:http://jsbin.com/oKezUkaz/1/

如果我们添加一个组(按“添加组”按钮),我们会在控制台中收到错误消息:

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

2 个答案:

答案 0 :(得分:1)

在这种情况下,您需要在两者上定义inverse,否则它会陷入沿着路径之一的循环中。 Ember Data的文档缺乏这一点,但应该在ED达到稳定的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

答案 1 :(得分:0)

您的模型必须像这样定义

 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')
        });

我无法理解你想要实现的目标,但我认为这是正确的方法。