2

我的 Json 数据如下所示:

 {"user":{"id":1,
        "name":"bob",
        "profile":{"id":1,
                "forename":"Foo",
                "surname":"Bar",
                "user_id":1}}

我的模型看起来像:

 App.User = DS.Model.extend({
     name: DS.attr('string'),
     profile: DS.belongsTo('App.Profile')
 });

 App.Profile = DS.Model.extend({
     forename: DS.attr('string'),
     surname: DS.attr('string'),,
     user: DS.belongsTo('App.User')
 });

当我尝试获取{{user.name}}时,它工作正常。user.profile.forename不工作。我尝试了我的用户模型

 DS.AuthenticatedRESTAdapter.map('App.User', {
     profile: {embedded: true}
 });

以及,但仍然没有工作。有什么建议么?

4

1 回答 1

1

缺少的是通过调用其“映射”函数来配置序列化程序(由适配器使用):

App.MySerializer = DS.Serializer.extend({
    init: function(){
      this._super();
      this.map(App.User, {
        profile: {
          embedded: 'load'
        }
      });
    }
});

您可以在这里找到 JFiddle 的工作示例。

于 2012-11-26T18:07:02.343 回答