我将自引用模型定义为:
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
最佳答案
在这种情况下,您需要在两者上定义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'})
});
关于javascript - 创建导致错误的自引用模型记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21269624/