3

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.
The relation between the two model is the following:

taskModel.creator_id = userModel.id   

Here is my code (1) (2)

The issue is the following (3):
When I try to fetch the models of task I get for the attributes.user the null value.


(1)

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

    relations: [
        {
            type: 'HasOne',
            key: 'user',
            relatedModel: User
        }
    ],

    urlRoot: 'url_get_tasks'
});

(2)

// UserModel
var User = Backbone.RelationalModel.extend({

    urlRoot: "url_get_users"

});

(3)

user = new User();
user.fetch();
console.log(user.attributes); // {id: 1, .....}

task = new Task();
task.fetch();
console.log(task.attributes); // {id: 12, creator_id: 1, user: null} ???

Why the task.attributes.user have the null value?

Lorraine Bernard
  • 13,000
  • 23
  • 82
  • 134

1 Answers1

0

Your relation on Task should be something like:

   {
       type: 'HasOne',
       key: 'user',
       keySource: 'creator_id',
       relatedModel: User
   }

(You're expecting it to use creator_id for the association but never told it that.)

philfreo
  • 41,941
  • 26
  • 128
  • 141