【问题标题】:Ember Data belongsTo Association (JSON format?)Ember 数据属于关联(JSON 格式?)
【发布时间】:2014-01-06 16:51:29
【问题描述】:

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

我正确设置了 Ember 模型 -- JS Fiddle -- 并且当我手动推入商店时关联工作。但是在请求 /publishers 索引时只创建发布者记录。

我尝试了几种类型的 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"
            }
        }
    ]
}

感谢您的帮助!

【问题讨论】:

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


    【解决方案1】:

    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/

    【讨论】:

    【解决方案2】:

    添加 ember-data 问题的链接以防它对任何人有所帮助 -- single object push payload

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多