2012-12-21 29 views
3

在這個應用程序中,事件是發生的事情,感覺是一個嵌套對象,描述你對它的感受。這裏是我的事件模型:骨幹關係沒有設置父模型或反向關係_id

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

它已經好幾個小時了,我不明白爲什麼這種關係不是由骨幹關係設定。

+0

倘使你能重現處於的jsfiddle :) – rcambrj

回答

0

您可能需要聲明BackboneRelational.HasOne對感情的模式,就像這樣:

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