Robin Wieruch
Robin Wieruch

Reputation: 15908

ember-data: how to get embedded json

My Json Data looks like this:

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

My model looks like:

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

and

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

When I try to get {{user.name}}, it works fine. user.profile.forename doesnt work. I tried for my user model

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

as well, but still doenst work. Any suggestions?

Upvotes: 2

Views: 657

Answers (1)

Rudi Angela
Rudi Angela

Reputation: 1483

What is missing is configuring the serializer (used by the adapter) by calling its 'map' function:

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

You can find a working example at JFiddle here.

Upvotes: 1

Related Questions