I have the following two models:
App.Domain = DS.Model.extend({
name: attr('string'),
users: DS.hasMany('App.User')
});
App.User = DS.Model.extend({
user: attr('string'),
domain: DS.belongsTo('App.Domain')
});
The Domain is loaded in the User record embeded:
DS.RESTAdapter.map('App.User', {
domain: { embedded: 'load' }
});
If I get the JSON data, ember-data will interpret that correctly.
GET /users
{
"users": [
{
"id": 1,
"domain_id": 1,
"user": "test",
"domain": {
"id": 1,
"name": "example.com"
}
}
]
}
My domains looks like this:
GET /domains
{
"domains": [
{
"id": 1,
"name": "example.com"
},
{
"id": 2,
"name": "example.org"
}
]
}
Now my question:
If I update the domain in a user record the domain_id foreignKey will not be updated.
If I try to commit this dirty record, ember will sending the old domain_id to the server. What is wrong in my code? Can I not update the belongsTo field for updating the foreignKey in this way?
>>var user = App.User.find(1);
>>user.get('domain.id')
1
>>var newDomain = App.Domain.find(2);
>>newDomain.get('id')
2
>>user.set('domain', newDomain)
>>user.get('isDirty')
true
>>user.get('domain.id')
2
>>user.get('domain_id')
1