不确定这是否是最好的方法,但我在玩,让它像这样工作:
完整来源| 跑
JavaScript:
App.DashboardView = Em.View.extend({
starActive: true,
userActive: false,
heartActive: false,
resetDisplay: function() {
this.set('starActive', false);
this.set('userActive', false);
this.set('heartActive', false);
},
star: function() {
console.log('star');
this.resetDisplay();
this.set('starActive', true);
},
user: function() {
console.log('user');
this.resetDisplay();
this.set('userActive', true);
},
heart: function() {
console.log('heart');
this.resetDisplay();
this.set('heartActive', true);
}
});
车把:
<script type="text/x-handlebars" data-template-name="_navDashboard">
<ul class="nav-list">
<li class="nav-header">Actions</li>
<li>
<a {{action star target="view"}}><i class="icon-star"></i> One</a>
</li>
<li>
<a {{action user target="view"}}><i class="icon-user"></i> Two</a>
</li>
<li>
<a {{action heart target="view"}}><i class="icon-heart"></i> Three</a>
</li>
</ul>
</script>
<script type="text/x-handlebars" data-template-name="dashboard/user">
<h3>User</h3>
</script>
<script type="text/x-handlebars" data-template-name="dashboard/star">
<h3>Star</h3>
</script>
<script type="text/x-handlebars" data-template-name="dashboard/heart">
<h3>Heart</h3>
</script>
<script type="text/x-handlebars" data-template-name="dashboard">
<div class="row-fluid">
<h2>Dashboard</h2>
</div>
<div class="row-fluid">
<div class="span3">
<div class="well sidebar-nav">
{{partial navDashboard}}
</div>
</div>
<div class="span7">
{{#if view.starActive}}
{{render "dashboard/star"}}
{{/if}}
{{#if view.userActive}}
{{render "dashboard/user"}}
{{/if}}
{{#if view.heartActive}}
{{render "dashboard/heart"}}
{{/if}}
</div>
</div>
</script>
我不确定这个解决方案,因为它让开发人员负责管理何时渲染或不渲染某个模板或视图;根据复杂性,可能会更容易出错并同时留下多个视图显示,甚至显示错误的视图,但这就是我目前得到的。