ember.js - How to use multiple models with a single route in EmberJS / Ember Data? -


from reading docs, looks have (or should) assign model route so:

app.postroute = ember.route.extend({     model: function() {         return app.post.find();     } }); 

what if need use several objects in route? i.e. posts, comments , users? how tell route load those?

last update forever: can't keep updating this. deprecated , way. here's better, , more up-to-date thread emberjs: how load multiple models on same route?

update: in original answer said use embedded: true in model definition. that's incorrect. in revision 12, ember-data expects foreign keys defined suffix (link) _id single record or _ids collection. similar following:

{     id: 1,     title: 'string',     body: 'string string string string...',     author_id: 1,     comment_ids: [1, 2, 3, 6],     tag_ids: [3,4] } 

i have updated fiddle , again if changes or if find more issues code provided in answer.


answer related models:

for scenario describing, rely on associations between models (setting embedded: true) , load post model in route, considering can define ds.hasmany association comment model , ds.belongsto association user in both comment , post models. this:

app.user = ds.model.extend({     firstname: ds.attr('string'),     lastname: ds.attr('string'),     email: ds.attr('string'),     posts: ds.hasmany('app.post'),     comments: ds.hasmany('app.comment') });  app.post = ds.model.extend({     title: ds.attr('string'),     body: ds.attr('string'),     author: ds.belongsto('app.user'),     comments: ds.hasmany('app.comment') });  app.comment = ds.model.extend({     body: ds.attr('string'),     post: ds.belongsto('app.post'),     author: ds.belongsto('app.user') }); 

this definition produce following:

associations between models

with definition, whenever find post, have access collection of comments associated post, , comment's author well, , user author of post, since embedded. route stays simple:

app.postspostroute = em.route.extend({     model: function(params) {         return app.post.find(params.post_id);     } }); 

so in postroute (or postspostroute if you're using resource), templates have access controller's content, post model, can refer author, author

<script type="text/x-handlebars" data-template-name="posts/post">     <h3>{{title}}</h3>     <div>by {{author.fullname}}</div><hr />     <div>         {{body}}     </div>     {{partial comments}} </script>  <script type="text/x-handlebars" data-template-name="_comments">     <h5>comments</h5>     {{#each content.comments}}     <hr />     <span>         {{this.body}}<br />         <small>by {{this.author.fullname}}</small>     </span>     {{/each}} </script> 

(see fiddle)


answer non-related models:

however, if scenario little more complex described, and/or have use (or query) different models particular route, recommend in route#setupcontroller. example:

app.postspostroute = em.route.extend({     model: function(params) {         return app.post.find(params.post_id);     },     // in sample, "model" instance of "post"     // coming model hook above     setupcontroller: function(controller, model) {         controller.set('content', model);         // "user_id" parameter can come global variable example         // or can implement in way.         // setup controller properties , models, or other models         // can used in route's template         controller.set('user', app.user.find(window.user_id));     } }); 

and when i'm in post route, templates have access user property in controller set in setupcontroller hook:

<script type="text/x-handlebars" data-template-name="posts/post">     <h3>{{title}}</h3>     <div>by {{controller.user.fullname}}</div><hr />     <div>         {{body}}     </div>     {{partial comments}} </script>  <script type="text/x-handlebars" data-template-name="_comments">     <h5>comments</h5>     {{#each content.comments}}     <hr />     <span>         {{this.body}}<br />         <small>by {{this.author.fullname}}</small>     </span>     {{/each}} </script> 

(see fiddle)


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -