创建引用错误的自我引用模型的记录 [英] creating records of self referring model causing error

查看:98
本文介绍了创建引用错误的自我引用模型的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我不知道如何定义逆属性来使事情工作。这是我拥有的jsbin: http://jsbin.com/oKezUkaz/1/



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

 断言失败:您在App.Answer上定义了childAnswers关系,但在App.Answer中找到了App.Answer类型的多个可能的反向关系。查看http://emberjs.com/guides/models/defining-models/#toc_explicit-inverses如何明确指定反转


解决方案

在这种情况下,您需要在两者中定义 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


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

解决方案

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

这篇关于创建引用错误的自我引用模型的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆