4
votes
App.User = DS.Model.extend({
  posts: DS.hasMany('post', {async: true})
});

App.Post = DS.Model.extend({
  body: DS.attr(),
  user: DS.belongsTo('user')
});

App.ProfileRoute = Ember.Route.extend({
  model: function(params) {
    return this.get('store').find('user', params.user_id)
  }
});

and in template

{{#each post in model.posts}}
  {{post.body}}
{{/each}}

json for user. I don't want embed posts in user json

{user: { posts: [1, 2, 3] }}

This don't render anything. It receives posts json from server after this error occur

Assertion failed: You looked up the 'posts' relationship on '' 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.attr({ async: true }))

In chrome inspector I see all data loaded properly. How can I solve this? Should I preload all models I want to use in templates?

2
I am having the same issue right now and I'm pretty stuck on this. :-(Alex

2 Answers

0
votes

The model function in your route is missing the return, so the error is being thrown when you try to access model.posts because there is no model.

App.ProfileRoute = Ember.Route.extend({
  model: function(params) {
    return this.get('store').find('user', params.user_id);
  }
});
0
votes

Have you tried to just write {{#each posts}}?

Worked for my project.

Then write {{body}} within the each block.

Let me know, thanks!