灰烬设计模式

时间:2013-06-07 18:30:47

标签: javascript ember.js

我正在开发一个基于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}));
        }    
    }    
});

是一个好的模式吗?

1 个答案:

答案 0 :(得分:1)

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