2016-03-04 68 views
0
exampleData = { 
    id: 1, 
    name: 'A', 
    house: { 
     address1: 'California' 
     address2: 'California' 
    } 
} 

House = Backbone.RelationalModel.extend({ 
    urlRoot: function urlRoot() { 
     var personId = this.get('person').id; // this.get('person') is null 

    } 
}); 


Person = Backbone.RelationalModel.extend({ 
    relations: [ 
     { // Create a (recursive) one-to-one relationship 
      type: Backbone.HasOne, 
      key: 'house', 
      relatedModel: House, 
      reverseRelation: { 
       type: Backbone.HasOne, 
       key: 'person' 
      } 
     } 
    ], 

    initialize: function() { 
    } 
}); 

我需要人的模型數據在衆議院模型,骨幹關係一對一關係,如何得到父母的模型數據?

但this.get(「人」)返回null,即使我設置了reverseRelation。

我怎樣才能得到人的數據在家裏?

+0

不應該你是在House模型中定義一個belongsTo關係? – Aukhan

+0

houseModel.get(「person」) – Farside

回答

0

我認爲你的代碼將不起作用。模型的定義有點不對。當你定義的關係,應該是參考對象,而不是字符串值:

relatedModel: House // instead of 'House' as a string 

然後你可以通過干將參考,從每個模型,都:

houseModel.get("person"); 
personModel.get("house");