gpt4 book ai didi

backbone.js - Backbone 关系不设置父模型,或反向关系_id

转载 作者:行者123 更新时间:2023-12-04 03:06:07 25 4
gpt4 key购买 nike

在此应用中,事件是发生的事情,感觉是描述您对此感受的嵌套对象。这是我的事件模型:

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

至此,我可以增删改查事件了,也有感触。但是,感受并没有被分配相反的关系。在某种意义上,属性'incident'被赋予值'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

现在已经好几个小时了,我不明白为什么关系没有被 backbone-relational 设置。

最佳答案

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

window.Feeling = Backbone.RelationalModel.extend({
urlRoot: '/incidents',
idAttribute: '_id',
relations:[{
type: Backbone.HasOne,
key: 'incident',
relatedModel: 'Incident'
}]
});

关于backbone.js - Backbone 关系不设置父模型,或反向关系_id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13982859/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com