gpt4 book ai didi

ember.js - 在 Ember 路由器中更改状态时断开 socket /删除 View ?

转载 作者:行者123 更新时间:2023-12-02 19:23:54 24 4
gpt4 key购买 nike

我有一个应用程序,其应用程序模板有两个导出。当我进入root.index状态时,我只想连接一个 socket ,当我进入root.chats状态时,我希望两个 socket 都连接。当我从 root.index 导航到 root.chats 时,这工作正常,但是当我导航回来时,navpane 导出仍然存在(因为它应该是)。我该如何断开此 socket 或删除最初连接的 View ? Controller 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();

最佳答案

我相信您正在寻找 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');
}
}),

关于ember.js - 在 Ember 路由器中更改状态时断开 socket /删除 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12995975/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com