Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Many-to-many relations not working #29

Closed
jonkoops opened this issue Dec 8, 2013 · 2 comments
Closed

Many-to-many relations not working #29

jonkoops opened this issue Dec 8, 2013 · 2 comments

Comments

@jonkoops
Copy link
Contributor

jonkoops commented Dec 8, 2013

App = Ember.Application.create();

App.Router.map(function() {

});

App.ApplicationAdapter = DS.LSAdapter.extend({
    namespace: 'ember-relations'
});

App.Post = DS.Model.extend({
    title: DS.attr(),
    tags: DS.hasMany('tag')
});

App.Tag = DS.Model.extend({
    title: DS.attr(),
    posts: DS.hasMany('post')
});

var FIXTURES = {
    'App.Post': {
        records: {
            '11': { id: '11', title: 'A post about HTML5', tags: [ '11' ] },
            '12': { id: '12', title: 'Some post about JavaScript and HTML5', tags: [ '11', '12' ] }
        }
    },
    'App.Tag': {
        records: {
            '11': { id: '11', title: 'HTML5', posts: [ '11', '12' ] },
            '12': { id: '12', title: 'JavaScript', posts: [ '12' ] }
        }
    }
};

localStorage.setItem('ember-relations', JSON.stringify(FIXTURES))

App.IndexRoute = Ember.Route.extend({
    model: function() {
        return this.store.find('post');
    }
});
    <h2>Posts:</h2>
    <ul>
        {{#each}}
            <li>
                {{title}}
                <ul>
                    <h3>Tags:</h3>
                    {{#each tags}}
                        <li>{{title}}</li>
                    {{else}}
                        <li>No tags associated with this post.</li>
                    {{/each}}
                </ul>
            </li>
        {{else}}
            <li>No posts found.</li>
        {{/each}}
    </ul>

The code results in the following exception: Assertion failed: You looked up the 'tags' relationship on '<App.Post:ember276:11>' but some of the associated records were not loaded. Either make sure they are all loaded together with the parent record, or specify that the relationship is async (DS.hasMany({ async: true }))

I'm using:
Ember 1.2.0
Ember Data 1.0.0-beta3
Handlebars 1.1.2
jQuery 1.10.2

@kurko
Copy link
Collaborator

kurko commented Jan 3, 2014

@jonkoops you need to set async: true in your relationships. In your Post model, define instead:

tags: DS.hasMany('tag', {async: true})

Even FixtureAdapter works like that. Also, I think this adapter is broken for anything past 1.0.0-beta1. I'm fixing it in #30.

@kurko kurko mentioned this issue Jan 3, 2014
@kurko
Copy link
Collaborator

kurko commented Feb 13, 2014

Please, check master again. #30 was merged. Please open a new issue if the problem persists.

@kurko kurko closed this as completed Feb 13, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants