अंतिम अपडेट हमेशा के लिए : मैं इसे अपडेट नहीं रख सकता। तो यह पदावनत है और इस तरह से होगा। यहाँ एक बेहतर, और अधिक अप-टू-डेट धागा EmberJS है: एक ही मार्ग पर कई मॉडल कैसे लोड करें?
अद्यतन: अपने मूल उत्तर में मैंने embedded: trueमॉडल परिभाषा में उपयोग करने के लिए कहा । यह गलत है। संशोधन 12 में, एम्बर-डेटा को विदेशी कुंजियों को एकल रिकॉर्ड या संग्रह के लिए एक प्रत्यय ( लिंक ) के साथ परिभाषित करने की उम्मीद है । निम्नलिखित के समान कुछ:_id_ids
{
id: 1,
title: 'string',
body: 'string string string string...',
author_id: 1,
comment_ids: [1, 2, 3, 6],
tag_ids: [3,4]
}
मैंने फ़िडेल को अपडेट किया है और यदि ऐसा कुछ भी बदलता है या यदि मुझे इस उत्तर में दिए गए कोड के साथ अधिक मुद्दे मिलते हैं तो मैं फिर से ऐसा करूंगा।
संबंधित मॉडल के साथ उत्तर दें:
परिदृश्य आप का वर्णन कर रहे हैं के लिए, मैं पर भरोसा होता संघों मॉडलों के बीच (सेटिंग embedded: true) और केवल लोड Postकि मार्ग में मॉडल, मैं एक परिभाषित कर सकते हैं पर विचार DS.hasManyके लिए संघ Commentमॉडल और DS.belongsToके लिए संघ Userदोनों में Commentऔर Postमॉडल। कुछ इस तरह:
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 would produce something like the following:

With this definition, whenever I find a Post, I will have access to a collection of comments associated with that post, and the comment's author as well, and the user which is the author of the post, since they are all embedded. The route stays simple:
App.PostsPostRoute = Em.Route.extend({
model: function(params) {
return App.Post.find(params.post_id);
}
});
So in the PostRoute (or PostsPostRoute if you're using resource), my templates will have access to the controller's content, which is the Post model, so I can refer to the author, simply as 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 with non-related models:
However, if your scenario is a little more complex than what you described, and/or have to use (or query) different models for a particular route, I would recommend to do it in Route#setupController. For example:
App.PostsPostRoute = Em.Route.extend({
model: function(params) {
return App.Post.find(params.post_id);
},
setupController: function(controller, model) {
controller.set('content', model);
controller.set('user', App.User.find(window.user_id));
}
});
And now when I'm in the Post route, my templates will have access to the user property in the controller as it was set up 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)