1

I would like to make a relation between two models User and Task using backbone-relational.
I would like for each Task to get the User model or User attributes.

The relation between the two models is the following:

taskModel.creator_id = userModel.id   

// TaskModel
var TaskModel = Backbone.RelationalModel.extend({

    relations: [
        {
            type: 'HasOne',
            key: 'creator_id',
            relatedModel: UserModel
        }
    ],

    urlRoot: 'url_get_tasks'
});
Lorraine Bernard
  • 13,000
  • 23
  • 82
  • 134

1 Answers1

0
relations: [
        {
            type: Backbone.HasOne,
            key: 'user',
            relatedModel: UserModel
        }
    ]

key attribute store initial data for related model. You can define parse method in TaskModel to make user: {id: 1} from creator_id: 1 on fetch or do it on in initialize:

this.set('user', {id: this.get('creator_id') })
Igor Alekseev
  • 1,208
  • 12
  • 20
  • It does not work, anyway looks to this question because the problem is related [Foreign key populated with an object](http://stackoverflow.com/questions/11346777/foreign-key-populated-with-an-object) – Lorraine Bernard Jul 05 '12 at 17:16