I have the following two models: User
and Job
.
Each user
can have just one job.
The user.attributes
and job.attributes
look like these (1):
(1)
user.attributes = {
id: 1,
name: 'barName',
job_id: 5
}
job.attributes = {
id: 5,
name: 'fooJob'
}
Let's suppose I want to make a relation between these two models:
The foreign key should be job_id
(2)
User = Backbone.ModelRelation.extend({
relations: [
{
type: Backbone.HasOne,
key: 'job_id',
keyDestination: 'job',
relatedModel: User
}
]
});
Using the code (2) the result will be:
(3)
user.attributes = {
id: 1,
name: 'barName',
job: job.attributes
}
As you can see the job_id
from user.attributes
is lost.
So, if I make a PUT request to the server, the server complains about the missing job_id attribute.
Any ideas how can I fix (3) in order to keep the job_id in user.attributes like (4)?
(4)
user.attributes = {
id: 1,
name: 'barName',
job_id: 5
job: job.attributes
}
Reference:
Paul Uithol - Backbone-relational