Ember-Data中的显式反转

时间:2014-09-12 17:06:34

标签: ember.js ember-data

我的模型看起来像这样:

App.Room = DS.Model.extend({
    title      : DS.attr('string'),
    description: DS.attr('string'),
    rooms      : DS.hasMany('room', {
        async  : true,
        inverse: 'parent'
    }),
    parent     : DS.belongsTo('room')
});

查看现有记录有效,但创建新记录除外。

我试过这样:

var self = this,
        parent = this.get('content'),
        input = this.getProperties('title', 'description'),
        newRoom = this.store.createRecord('room', {
            title      : input.title,
            description: input.description,
            parent  : parent
        });

parent.get('rooms').then(function (rooms) {
    rooms.pushObject(newRoom);
    newRoom.save();
    parent.save();
    self.transitionToRoute('rooms');
});

但是得到这个错误:

Assertion Failed: You defined the 'parent' relationship on (subclass of DS.Model),
but multiple possible inverse relationships of type (subclass of DS.Model) were
found on (subclass of DS.Model).

1 个答案:

答案 0 :(得分:1)

你将一个物体与自身联系起来,所以很可能Ember-Data的反向猜测算法变得混乱了。明确地声明两者的反转。

App.Room = DS.Model.extend({
    rooms: DS.hasMany('room', {
        async: true,
        inverse: 'parent'
    }),
    parent: DS.belongsTo('room', {
        inverse: 'rooms'
    })
});

因为它特别提到parent关系,我假设Ember-Data认为它可能是它自己的逆。