0

I've tried to retrieve some data using ember-data. But there is just artists: undefined when I look in the console the model object retrieved.

I don't understand why the association is not done, here is my code

MyApp.Track = DS.Model.extend
    artists: DS.hasMany 'MyApp.Artist'
    name: DS.attr 'string'

MyApp.Artist = DS.Model.extend
    name: DS.attr 'string'

And there is the data should be loaded :

{
    "track": {
    "name": "Troublemaker",
    "id": 74,
    "artists": [
        {
            "artist": {
                 "name": "Taio Cruz"
            }
        }
    ]
  }
}

I've got the name of the track but not the id and the artistsdata.

Thanks a lot for your help.

4

1 回答 1

1

您必须将艺术家的 id 作为artist_ids 数组返回

这是该http://emberjs.com/guides/models/the-rest-adapter/#toc_relationships的文档

此外,如果您打算发出一个 ajax 请求,则必须包含艺术家的 id 才能工作http://emberjs.com/guides/models/the-rest-adapter/#toc_sideloaded-relationships

如果您的后端在轨道上,则必须在模型序列化程序中包含 ID,如下所示:

class TrackSerializer < ActiveModel::Serializer
  attributes :id, :name, :actist_ids
end
于 2013-04-17T14:04:48.463 回答