relational not setting parent model, or reverse relation

In this app, an incident is something that happened, and a feeling is a nested object that describes how you felt about it. Here's my Incident model:

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

And here is the Feeling model:

window.Feeling = Backbone.RelationalModel.extend({
  urlRoot: '/incidents',
  idAttribute: '_id'
});

At this point, I can CRUD incidents, and also feelings. But, feelings are not being assigned the reverse relation. In a feeling, the attribute 'incident' is given the value 'null'. In my MongoDB collection, I get these two unrelated objects:

{ "description" : "I feel sad", "_id" : ObjectId("50d3b1462ff17f07cf000002") }
{ "name" : "asdf", "intensityBefore" : "asdf", "intensityAfter" : "asdf", "incident" : null, "_id" : ObjectId("50d3b14e2ff17f07cf000003") }

I have the full project up at https://github.com/mhurwi/cbt-app/tree/relational.

Note, this app is built off a starter app by Christophe Coenraets: https://github.com/ccoenraets/nodecellar

It's been many hours now, and I cannot understand why the relationship is not being set by backbone-relational.


您可能需要在Feelings模型上声明BackboneRelational.HasOne,如下所示:

window.Feeling = Backbone.RelationalModel.extend({
    urlRoot: '/incidents',
    idAttribute: '_id',
    relations:[{
        type: Backbone.HasOne,
        key: 'incident',
        relatedModel: 'Incident'
    }]
});
链接地址: http://www.djcxy.com/p/65528.html

上一篇: 骨干事件不会在Safari中触发

下一篇: 关系式不设置父模型或反向关系