Skip to content

Commit d9c1bbf

Browse files
committed
updates #1691: Use listenTo
so that we have automatic event deregistration when the model gets removed.
1 parent b52b3e5 commit d9c1bbf

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

src/headless/converse-muc.js

+24-6
Original file line numberDiff line numberDiff line change
@@ -264,30 +264,48 @@ converse.plugins.add('converse-muc', {
264264
},
265265

266266
onOccupantRemoved (occupant) {
267+
this.stopListening(this.occupant);
267268
delete this.occupant;
268269
const chatbox = _.get(this, 'collection.chatbox');
269-
chatbox.occupants.on('add', this.onOccupantAdded, this);
270+
if (!chatbox) {
271+
return _converse.log(
272+
`Could not get collection.chatbox for message: ${this.get('id')}`,
273+
Strophe.LogLevel.ERROR
274+
);
275+
}
276+
this.listenTo(chatbox.occupants, 'add', this.onOccupantAdded);
270277
},
271278

272279
onOccupantAdded (occupant) {
273280
if (occupant.get('nick') === Strophe.getResourceFromJid(this.get('from'))) {
274281
this.occupant = occupant;
275-
this.occupant.on('destroy', this.onOccupantRemoved, this);
282+
this.listenTo(this.occupant, 'destroy', this.onOccupantRemoved);
276283
const chatbox = _.get(this, 'collection.chatbox');
277-
chatbox.occupants.off('add', this.onOccupantAdded, this);
284+
if (!chatbox) {
285+
return _converse.log(
286+
`Could not get collection.chatbox for message: ${this.get('id')}`,
287+
Strophe.LogLevel.ERROR
288+
);
289+
}
290+
this.stopListening(chatbox.occupants, 'add', this.onOccupantAdded);
278291
}
279292
},
280293

281294
setOccupant () {
282295
if (this.get('type') !== 'groupchat') { return; }
283296
const chatbox = _.get(this, 'collection.chatbox');
284-
if (!chatbox) { return; }
297+
if (!chatbox) {
298+
return _converse.log(
299+
`Could not get collection.chatbox for message: ${this.get('id')}`,
300+
Strophe.LogLevel.ERROR
301+
);
302+
}
285303
const nick = Strophe.getResourceFromJid(this.get('from'));
286304
this.occupant = chatbox.occupants.findWhere({'nick': nick});
287305
if (this.occupant) {
288-
this.occupant.on('destroy', this.onOccupantRemoved, this);
306+
this.listenTo(this.occupant, 'destroy', this.onOccupantRemoved);
289307
} else {
290-
chatbox.occupants.on('add', this.onOccupantAdded, this);
308+
this.listenTo(chatbox.occupants, 'add', this.onOccupantAdded);
291309
}
292310

293311
},

0 commit comments

Comments
 (0)