Ember Data belongsTo Association (JSON format?)

这一生的挚爱 提交于 2019-12-24 13:08:04

问题


I have two models 'Author' and 'Publisher' (Rails), with a publisher hasOne author / author belongsTo publisher relationship.

I have the Ember models setup correctly -- JS Fiddle -- and the associations working when I manually push into the store. But only the publisher records are created when requesting /publishers index.

I've tried several types of JSON responses:

Publishers with author

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

Publishers with authors

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

Publishers with author embedded

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

Thanks for any help!


回答1:


The ActiveModelAdapter/ActiveModelSerializer expects _id/_ids to be appended on relationships

{
    "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/




回答2:


Adding a link to ember-data issue in case it helps anyone -- single object push payload



来源:https://stackoverflow.com/questions/20954980/ember-data-belongsto-association-json-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!