[英]get a model from other controller in emberjs in view
I got a problem with a bigger project in ember, I want to get information from a model when i'm in a template that is not associated with the controller for that model. 我在ember中遇到了一个更大项目的问题,当我在一个与该模型的控制器无关的模板时,我想从模型中获取信息。
I got these templates: 我有这些模板:
<script type="text/x-handlebars" data-template-name="community">
{{model.name}}
{{outlet}}
</script>
//users is a subroute from community
<script type="text/x-handlebars" data-template-name="communityUsers">
//assume i want to display something from a community here like:
{{community.id}}
{{#each user in model}}
<li>{{user.name}}</li>
{{/each}}
</script>
in the routes I fetch the appropriate models so for the community i got 1 community and in the communityUsers i have an array with users 在路线中我获取了适当的模型,所以对于社区我有1个社区,在社区用户我有一个用户数组
does anyone know the best solution for this? 有谁知道这个的最佳解决方案?
So from what I've understood, you are trying to access the model of communityController
inside the template of it's child template communityUsers
. 因此,根据我的理解,您正在尝试在其子模板communityUsers
的模板中访问communityController
的模型。 For that you've to define your communityUsersController
to need the communityController
by 对于您定义您communityUsersController
需要的communityController
通过
needs: ['community']
and then in your template 然后在你的模板中
{{#each user in controllers.community.model}}
<li>{{user.name}}</li>
{{/each}}
I got a problem with a bigger project in ember, I want to get information from a model when i'm in a template that is not associated with the controller for that model. 我在ember中遇到了一个更大项目的问题,当我在一个与该模型的控制器无关的模板时,我想从模型中获取信息。
Assuming you get your communities like this: 假设你得到这样的社区:
App.CommunityRoute = Ember.Route.extend({
model: function() {
return App.Community.find();
}
});
Further assuming you want to have access from a controller that is not related to your CommunityController
(which get's it's content set after the model hook returns) you could use the needs
API and define a dependance to it 进一步假设您希望从与CommunityController
无关的控制器访问(在模型钩子返回后得到它的内容集),您可以使用needs
API并定义对它的依赖性
App.CommunityUsersController = Ember.Objectontroller.extend({
// here dependence definition
needs: ['community'],
// create an observer that returns the community you want
// I've chosen just the first one
choosenCommunity: function() {
return this.get('controllers.community').objectAt(0);
}.observes('controllers.community')
});
So now in your communityUsers
template you are able to access those properties 现在,在您的communityUsers
模板中,您可以访问这些属性
<script type="text/x-handlebars" data-template-name="communityUsers">
//assume i want to display something from a community here like:
{{choosenCommunity.id}}
{{#each user in choosenCommunity.users}}
<li>{{user.name}}</li>
{{/each}}
</script>
And the best of all this, everything will stay up to date since it's bound. 最重要的是,一切都将保持最新,因为它受到约束。
Hope it helps. 希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.