I'm new to Emberjs, and I'm trying to get my head around how to get data out of my model.

Here's the data structure as its being returned from my server

candidate: {
    image: [image],
    user: {
         name: [User Name]
    }, 
    agent: {
        phone: [phone]
        team: {
            name: [Team Name]
        }
        user: {
            name: [User Name]
        }
    }
}

I cant get ember to recognize any associations more than one level deep.

heres my candidate controller

App.CandidateController = Ember.ObjectController.extend({
   location: function() {
        var address = this.get('address');
        var city = this.get('city');
        var state = this.get('state');
        var zip = this.get('zip');
        return address + ' ' + city + ' ' + state + ', ' + zip;
    }.property('address', 'city', 'state', 'zip')
});

App.CandidateSerializer = DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
    attrs: {
        agent: {embedded: 'always'},
        user: {embedded: 'always'}
    }
});

This allows me to get one level deep, but I need two or three levels of association.

Is there a different way to go about it?

I thought organizing everything on the server the way I want it displayed, and just returning the data as its supposed to be rendered to ember, but I have a feeling I'll run into problems when trying to update the models.

Any help is appreciated.

/****** UPDATE *******/

I have refactored my code and I am now returning data from the serializer like this:

{
    candidate: {
        // relevant fields
    },
    agent: {
        //relevant fields
    },
    team: {
        // relevant fields
    }
    user: [
        {  //  candidate user fields    },
        {  //  agent user fields    }
    ]
};

However, data is still not available in my template. In the Ember chrome extension, in the data tab, I get

candidate: (1)
agent: (0)
team: (0)
user (2) 

I can see candidate and user data, but not agent or team data. In my candidate model, I have:

App.Candidate = DS.Model.extend({
    // other fields
    user_id: DS.attr('number'),
    agent_id: DS.attr('number'),
    user: DS.belongsTo('user'),
    agent: DS.belongsTo('agent')
});

It doesn't seem like the belongsTo association actually does anything.

So the first issue is that I'm not getting the correct data, the second issue, and the one that makes me think I am going about this incorrectly, is that I have two users information that I need to display in the template. The first is user information associated with the candidate, and the second is user information that is associated with the agent. Both data need to appear in the same template. Since there is no hierarchy to the data, how would the template know which user info to display in each location?

Again, I think Im thinking about this whole thing incorrectly.

Thanks for your help.

Related posts

Recent Viewed