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() {
    }
});

I need to get the person's model data in House model,

but this.get('person') returns null, even I set up the reverseRelation.

How can I get the Person's data in house?

Expert wanna be
  • 10,218
  • 26
  • 105
  • 158

1 Answers1

0

I think your code will not work. The definition of model is a bit wrong. When you define relation, it should be reference to the object, not the string value:

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

Then you may reference through the getters, from each of the models, alike:

houseModel.get("person");
personModel.get("house");
Farside
  • 9,923
  • 4
  • 47
  • 60