Access relationships from templates with ember-data 1.0.0 beta

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 the template

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

json for the user. I do not want to embed messages in user json

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

It does not display anything. It receives json messages from the server after this error occurs.

Assertion failed: you looked at the "posts" relationship on "", but some related posts were not loaded. Either make sure that they are all loaded with the parent record, or indicate that the relation is async ( DS.attr({ async: true }) )

In the chrome inspector, I see that all the data is loaded properly. How can i solve this? Do I have to preload all the models that I want to use in the templates?

+4
source share
2 answers

The model function on your route does not matter return , so when you try to access model.posts an error occurs because there is no model.

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

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

Worked for my project.

Then write {{body}} in each block.

Let me know, thanks!

0
source

Source: https://habr.com/ru/post/1500661/


All Articles