关系式不设置父模型或反向关系
在这个应用程序中,事件是发生的事情,而感觉是一个嵌套的对象,描述你对它的感受。 这是我的事件模型:
window.Incident = Backbone.RelationalModel.extend({
urlRoot: "/incidents",
idAttribute: "_id",
relations:[{
    type: Backbone.HasMany,
    key: 'feelings',
    relatedModel: 'Feeling',
    reverseRelation: {
        key: 'incident',
        includeInJSON: '_id'
    }
},
{
    type: Backbone.HasMany,
    key: 'thoughts',
    relatedModel: 'window.Thought',
    reverseRelation: {
        key: 'incident',
        includeInJSON: '_id'
    }
}],
 // rest of model...
});
这里是感觉模型:
window.Feeling = Backbone.RelationalModel.extend({
  urlRoot: '/incidents',
  idAttribute: '_id'
});
在这一点上,我可以CRUD事件,也可以感受。 但是,感情并没有被赋予相反的关系。 感觉上,'事件'属性的值为'null'。 在我的MongoDB集合中,我得到了这两个不相关的对象:
{ "description" : "I feel sad", "_id" : ObjectId("50d3b1462ff17f07cf000002") }
{ "name" : "asdf", "intensityBefore" : "asdf", "intensityAfter" : "asdf", "incident" : null, "_id" : ObjectId("50d3b14e2ff17f07cf000003") }
我有完整的项目在https://github.com/mhurwi/cbt-app/tree/relational。
请注意,这个应用程序是由Christophe Coenraets构建的一款初级应用程序:https://github.com/ccoenraets/nodecellar
现在已经很多小时了,我不明白为什么这种关系不是由骨干关系设置的。
您可能需要在Feelings模型上声明BackboneRelational.HasOne,如下所示:
window.Feeling = Backbone.RelationalModel.extend({
    urlRoot: '/incidents',
    idAttribute: '_id',
    relations:[{
        type: Backbone.HasOne,
        key: 'incident',
        relatedModel: 'Incident'
    }]
});
上一篇: relational not setting parent model, or reverse relation
