1
我有一個名爲users.js
的模型及相關控制器和路由的模型應用。在我的usersController.js
中,我有一個功能來計算系統中的用戶數量。然後,我可以在我的users
模板中顯示此圖。但是,我想在我的index
模板中顯示該數字,請問這是可能的嗎?我將如何去做 - 現在這個數字似乎沒有在我的users
模型之外使用。在ember.js中訪問其模型之外的計算屬性
這裏是我的usersController-
App.UsersController = Ember.ArrayController.extend({
sortProperties: ['name'],
sortAscending: true,
numUsers: function() {
return this.get('model.length');
}.property('model.[]')
});
而我的HTML的
<script type = "text/x-handlebars" id = "index">
<h2>Homepage</h2>
//This is where I would like the figure to be
<h3>There are {{numUsers}} users </h3>
</script>
<script type = "text/x-handlebars" id = "users">
<div class="col-md-2">
{{#link-to "users.create"}}<button type="button" class="btn btn-default btn-lg"><span class="glyphicon glyphicon-plus"></button> {{/link-to}}
//This works fine
<div>Users: {{numUsers}}</div>
</div>
<div class="col-md-10">
<ul class="list-group">
{{#each user in controller}}
<li class="list-group-item">
{{#link-to "user" user}}
{{user.name}}
{{/link-to}}
</li>
{{/each}}
</ul>
{{outlet}}
</div>
</script>
輝煌,謝謝 - 如果我想爲多個模型做這個工作,請說明主題計數,IndexRoute和IndexController會是什麼樣子? – bookthief
不客氣。我更新了答案,請看看。 –