简体   繁体   English

如何更新余烬数据中的关系

[英]How to update relationships in ember-data

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: Domain已加载到嵌入的User记录中:

DS.RESTAdapter.map('App.User', {
    domain: { embedded: 'load' }
});

If I get the JSON data, ember-data will interpret that correctly. 如果我得到JSON数据,则ember-data将正确地解释该数据。

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. 如果我更新domainuser记录domain_id外键将不会被更新。 If I try to commit this dirty record, ember will sending the old domain_id to the server. 如果我尝试提交此脏记录,则ember会将旧的domain_id发送到服务器。 What is wrong in my code? 我的代码有什么问题? Can I not update the belongsTo field for updating the foreignKey in this way? 我不能以这种方式更新用来更新foreignKeybelongsTo字段吗?

>>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

我认为您需要首先提交,以更新domain_id。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM