0

我正在做一个基于 ember 的项目,但我不确定设计。我有一个所有其他控制器都在扩展的应用程序控制器:

基本控制器

App.ApplicationController = Ember.Controller.extend({
    needs:['userNav'],
    dateNavSelected : '',
    currentSleeperImg:AppStateManager.get('currentSleeperImg'),
    currentSleeperName:AppStateManager.get('currentSleeperName'),        
    didSleeperChange:false,
    logout: function(){
        AppStateManager.transitionTo('loggedOutState');
        this.transitionToRoute('signin');
    },
    selectSleeper: function(sleeper){ //Gets the sleeper change from the menu
        var currSleeper = AppStateManager.get('currentSleeperObj');
        if(!currSleeper || (currSleeper && sleeper.get('id') != currSleeper.get('id'))){
            AppStateManager.set('currentSleeperImg', sleeper.get('sleeper_avatar'));
            AppStateManager.set('currentSleeperName', sleeper.get('sleeper_name'));
            AppStateManager.set('currentSleeperObj', sleeper );
            this.set('currentSleeperImg' , sleeper.get('sleeper_avatar'));
            this.set('currentSleeperName' , sleeper.get('sleeper_name'));
            this.set('didSleeperChange', true);
        }else{
            this.set('didSleeperChange', false);
        }
    }
});

子控制器

App.LastNightSummaryController = App.ApplicationController.extend({        
    pageTitle: moment().subtract('days', 1).format("MMM D"),
    active:'last-night-summary',        
    selectSleeper: function(sleeper){ //Gets the sleeper change from the menu
        this._super(sleeper);   
        if(this.get('didSleeperChange')){
            this.set('content', App.SleepData.find({id:sleeper.get('id'), sleeper:sleeper}));
        }    
    }    
});

是好的模式吗?

4

1 回答 1

1

不,您应该从基本控制器扩展。应用程序控制器已经有责任跟踪应用程序状态。如果您出于任何原因需要访问应用程序控制器,请在代码中使用 @controllerFor("application")。

于 2013-06-07T23:39:23.677 回答