1
votes

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

1 Answers

0
votes
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') })