对于这类问题,我通常Ember.StateManager
从现在单独的ember-states
项目中创建一个。然后,我在控制器上计算了属性,以确定是否应显示页面的各个部分。然后,在我的模板中,我使用仅在处于给定状态{{#if shouldShowPartX}} ... {{\if}}
时才显示的类型语句。StateManager
这是一个更完整的示例:
App.MyController = Ember.Controller.extend({
isOpen: Ember.computed.equal('panelManager.currentState.name', 'open')
init: function() {
this._super();
# Create the state manager instance from the class defined on this controller.
this.set('panelManager', this.PanelManager.create());
},
reset: (function() {
this.get('panelManager').send('reset');
}).on('init'),
PanelManager: Ember.StateManager.extend({
initialState: 'open',
open: Ember.State.create({
# If already open do nothing.
open: function(manager) {},
# Close the panel
shrink: function(manager) {
manager.transitionTo('closed');
},
}),
closed: Ember.State.create({
# If already closed, do nothing
shrink: function() {},
# Open the panel
open: function(manager) {
manager.transitionTo('open');
},
}),
reset: function(manager) {
manager.transitionTo(manager.get('initialState'));
}
})
});
然后在我看来,我可以做类似的事情:
{{#if isOpen}}
<div class="panel panel-open"> ... </div>
{{else}}
<div class="panel panel-closed"> ... </div>
{{/if}}
这是一个公认的简单示例,通常不会将 aStateManager
用于简单的两态情况,但更复杂的示例可能会令人困惑。
这会有帮助吗?它不需要直接弄乱 aContainerView
而是依赖于隐式ContainerView
处理{{#if}}
块并根据StateManager
.