Ember Data属于协会(JSON格式?)

时间:2014-01-06 16:51:29

标签: javascript ruby-on-rails ember.js ember-data active-model-serializers

我有两个模型'作者'和'发布者'(Rails),发布者有一个作者/作者所属的发布者关系。

我正确设置了Ember模型 - JS Fiddle - 以及当我手动推入商店时的关联。但是,在请求/发布者索引时,只会创建发布者记录。

我尝试了几种类型的JSON响应:

有作者的出版商

{
    "publishers": [
        {
            "id": 1,
            "name": "Test P 1",
            "author": 1
        }
    ],
    "author": {
        "id": 1,
        "name": "Test A 1",
        "publisher": 1
    }
}

有作者的出版商

{
    "publishers": [
        {
            "id": 1,
            "name": "Test P 1",
            "author": 1
        }
    ],
    "authors": [{
        "id": 1,
        "name": "Test A 1",
        "publisher": 1
    }]
}

嵌入了作者的发布商

{
    "publishers": [
        {
            "id": 1,
            "name": "Test P 1",
            "author": {
              "id": 1
              "name": "Test A 1"
            }
        }
    ]
}

感谢您的帮助!

2 个答案:

答案 0 :(得分:2)

ActiveModelAdapter / ActiveModelSerializer期望_id / _ids附加在关系上

{
    "publishers": [
        {
            "id": 1,
            "name": "Test P 1",
            "author_id": 1
        }
    ],
    "authors": [{
        "id": 1,
        "name": "Test A 1",
        "publisher_id": 1
    }]
}

http://jsfiddle.net/6Z2AL/1/

答案 1 :(得分:0)

添加指向ember-data问题的链接,以防有人帮助 - single object push payload