Commit ed4b9a85 authored by JC Brand's avatar JC Brand

Remove ChatBox views when they get closed.

Previously views were only hidden, but not removed. This was an unnecessary
"optimization" which introduced unnecessary complexity.

Problem solved was that closing minimized chats didn't dereference the model
for hidden "normal" chat views, causing an exception to be raised when trying
to log out.
parent f353fe86
...@@ -765,11 +765,14 @@ ...@@ -765,11 +765,14 @@
close: function (ev) { close: function (ev) {
if (ev && ev.preventDefault) { ev.preventDefault(); } if (ev && ev.preventDefault) { ev.preventDefault(); }
if (converse.connection.connected) { if (converse.connection.connected) {
// Immediately sending the chat state, because the
// model is going to be destroyed afterwards.
this.model.set('chat_state', converse.INACTIVE);
this.sendChatState();
this.model.destroy(); this.model.destroy();
this.setChatState(converse.INACTIVE);
} else {
this.hide();
} }
this.remove();
converse.emit('chatBoxClosed', this); converse.emit('chatBoxClosed', this);
return this; return this;
}, },
......
...@@ -1411,6 +1411,7 @@ ...@@ -1411,6 +1411,7 @@
initialize: function () { initialize: function () {
this.model.on("add", this.onChatBoxAdded, this); this.model.on("add", this.onChatBoxAdded, this);
this.model.on("destroy", this.removeChat, this);
}, },
_ensureElement: function () { _ensureElement: function () {
...@@ -1432,7 +1433,12 @@ ...@@ -1432,7 +1433,12 @@
onChatBoxAdded: function (item) { onChatBoxAdded: function (item) {
var view = this.get(item.get('id')); var view = this.get(item.get('id'));
// Views aren't created here, since the core code doesn't have
// contain any views. Instead, they're created in overrides in
// converse-chatiew.js and/or converse-muc.js
if (view) { if (view) {
// This is an optimization. We don't remove older views, so
// when one is available, we reuse it.
delete view.model; // Remove ref to old model to help garbage collection delete view.model; // Remove ref to old model to help garbage collection
view.model = item; view.model = item;
view.initialize(); view.initialize();
...@@ -1440,6 +1446,10 @@ ...@@ -1440,6 +1446,10 @@
return view; return view;
}, },
removeChat: function (item) {
this.remove(item.get('id'));
},
closeAllChatBoxes: function () { closeAllChatBoxes: function () {
/* This method gets overridden in src/converse-controlbox.js if /* This method gets overridden in src/converse-controlbox.js if
* the controlbox plugin is active. * the controlbox plugin is active.
......
...@@ -223,8 +223,15 @@ ...@@ -223,8 +223,15 @@
close: function (ev) { close: function (ev) {
if (ev && ev.preventDefault) { ev.preventDefault(); } if (ev && ev.preventDefault) { ev.preventDefault(); }
this.remove(); this.remove();
var view = converse.chatboxviews.get(this.model.get('id'));
if (view) {
// This will call model.destroy(), removing it from the
// collection and will also emit 'chatBoxClosed'
view.close();
} else {
this.model.destroy(); this.model.destroy();
converse.emit('chatBoxClosed', this); converse.emit('chatBoxClosed', this);
}
return this; return this;
}, },
......
...@@ -192,9 +192,6 @@ ...@@ -192,9 +192,6 @@
this.maximize(); this.maximize();
} }
}, this); }, this);
this.model.on('destroy', function () {
this.hide().leave();
}, this);
this.occupantsview = new converse.ChatRoomOccupantsView({ this.occupantsview = new converse.ChatRoomOccupantsView({
model: new converse.ChatRoomOccupants({nick: this.model.get('nick')}) model: new converse.ChatRoomOccupants({nick: this.model.get('nick')})
...@@ -243,6 +240,12 @@ ...@@ -243,6 +240,12 @@
return this; return this;
}, },
close: function (ev) {
converse.connection.deleteHandler(this.handler);
this.leave();
converse.ChatBoxView.prototype.close.apply(this, arguments);
},
toggleOccupants: function (ev, preserve_state) { toggleOccupants: function (ev, preserve_state) {
if (ev) { if (ev) {
ev.preventDefault(); ev.preventDefault();
...@@ -545,7 +548,9 @@ ...@@ -545,7 +548,9 @@
presence.c("status", exit_msg); presence.c("status", exit_msg);
} }
converse.connection.addHandler( converse.connection.addHandler(
function () { this.model.set('connection_status', Strophe.Status.DISCONNECTED); }.bind(this), function () {
this.model.set('connection_status', Strophe.Status.DISCONNECTED);
}.bind(this),
null, "presence", null, presenceid); null, "presence", null, presenceid);
converse.connection.send(presence); converse.connection.send(presence);
}, },
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment