3

2 つのアウトレットを持つアプリケーション テンプレートを使用したアプリケーションがあります。root.index 状態に入るときは 1 つのアウトレットだけを接続し、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();
4

1 に答える 1

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');
    }
}),
于 2012-10-21T18:48:21.150 に答える