在更改Ember路由器中的状态时断开插座/删除视图?

时间:2012-10-21 07:50:27

标签: ember.js ember-old-router

我有一个带有两个出口的应用程序模板的应用程序。当我进入root.index状态时,我只想连接一个插座,当我进入root.chats状态时,我希望连接两个插座。当我从root.index导航到root.chats时这很好用,但是当我向后导航时,navpane出口仍然存在(应该是这样)。如何断开此插座或取消首先连接的视图?是否弃用了控制器mixin的disconnectOutlet方法?我应该完全不同地构建这个吗?我在下面提供了我的代码。感谢。

// State flag helper function. See https://github.com/ghempton/ember-router-example/blob/master/js/app.js

function stateFlag(name) {
  return Ember.computed(function() {
    var state = App.router.currentState;
    while(state) {
      if(state.name === name) return true;
      state = state.get('parentState');
    }
    return false;
  }).property('App.router.currentState');
}

// Application

App = Em.Application.create({

    ApplicationController: Ember.Controller.extend({
        isChats: stateFlag('chats')
    }),
    ApplicationView: Ember.View.extend({
        templateName: 'application'
    }),
    ChatlistController:  Em.Controller.extend({
        hideView: false
    }),
    ChatlistView:  Em.View.extend({
        templateName:  'chatlist',
        didInsertElement: function(){
            this.$("#nav_pane").css({'left':'-=275', 'z-index':'-5'}).animate({
                left: ["+=275", 'swing'],
            },500,function() {
                $(this).css('z-index','5')
            });
        },
        _hideViewChanged: function() {
            if (this.get('hideView')) {
                this.hide();
            }
        }.observes('hideView'),
        hide: function() {
            var that = this;
            this.$("#nav_pane").hide("slow", function() {
                that.remove();
            });
        }
    }),
    ChatroomController:  Em.Controller.extend({

    }),
    ChatroomView:  Em.View.extend({
        templateName:  'chatroom'
    }),
    DashboardController:  Em.Controller.extend({

    }),
    DashboardView:  Em.View.extend({
        templateName:  'dashboard'
    }),
    Router: Ember.Router.extend({
        //location: 'history',
        enableLogging: true,
        goToDashboard:  Ember.Route.transitionTo('root.index'),
        goToChats:  Ember.Route.transitionTo('root.chats'),
        root:  Ember.Route.extend({
            index:  Ember.Route.extend({
                route:  '/',
                connectOutlets: function(router, context) {
                    router.get('applicationController').connectOutlet('content', 'dashboard');
                }
            }),
            chats:  Ember.Route.extend({
                route:  '/chats',
                connectOutlets: function(router, context) {
                    router.get('applicationController').connectOutlet('navpane', 'chatlist');
                    router.get('applicationController').connectOutlet('content', 'chatroom');
                }
            }),
            files:  Ember.Route.extend({
                route:  '/files'
            })
        })
    })
});

App.initialize();

1 个答案:

答案 0 :(得分:12)

我相信您正在寻找disconnectOutlet,这已完全弃用:http://emberjs.com/api/classes/Ember.Controller.html#method_disconnectOutlet

生活的好地方是路线的exit事件:http://emberjs.com/api/classes/Ember.State.html#event_exit

chats:  Ember.Route.extend({
    route:  '/chats',
    connectOutlets: function(router, context) {
        router.get('applicationController').connectOutlet('navpane', 'chatlist');
        router.get('applicationController').connectOutlet('content', 'chatroom');
    },
    exit: function(router){
      router.get('applicationController').disconnectOutlet('chatroom');
    }
}),