Quantcast
Channel: Ember.JS - Latest topics
Viewing all articles
Browse latest Browse all 4828

Can't access lazy loaded relationships

$
0
0

@samsebastian wrote:

I've been staring at this for hours and am getting nowhere :frowning:

I am using the JSONAPIAdapter with a JSONAPI compliant API.

When the route is hit the data is being loaded successfully to both User and Code models, but when I try to access the relation in a template, the data from User is presented but I can't get to anything in Code

My models look like:

// app/models/code.js

export default DS.Model.extend({
    // Attributes

    title:      DS.attr('string'),
    created_at: DS.attr('date'),
    updated_at: DS.attr('date'),
    
    // Relationships
    users: DS.hasMany('user', {async: true})
    
});

//app/models/user.js

export default DS.Model.extend({
    // Attributes

    first_name:   DS.attr('string'),
    last_name:    DS.attr('string'),
    phone:   DS.attr('number'),
    created_at:   DS.attr('date'),
    updated_at:   DS.attr('date'),

    // Relationships

    codes: DS.hasMany('code', {async: true})
    
});

My route looks like:

// app/pods/users/index/route.js

export default Ember.Route.extend({
    model() {
        return this.store.query('user', {include: 'codes'});
    }
});

My template looks like:

// app/pods/users/index/template.hbs

<ul>
            {{#each model as |user|}}
                <li>
                {{user.first_name}}
                    <ul>
                        {{#each user.codes as |code|}}
                            <li>{{code.title}}</li>
                        {{/each}}
                    </ul>
                </li>

            {{/each}}
            </ul>

I feel like I'm doing something wrong with the way I'm access the codes, but all the guides I find are from ~2014..

Would be awesome if anyone had some pointers on where to look from here?

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 4828

Trending Articles