From 23a1dc4f2b26d74f3bb77c9229143b61f2b350ba Mon Sep 17 00:00:00 2001 From: JC Brand Date: Tue, 1 Mar 2016 22:26:36 +0000 Subject: [PATCH] Some fixes concerning event emitting. Don't make the event "this" context the protected converse object. Can't trigger multiple data parameters, need to pass an object if there are multiple values to be sent. --- docs/source/development.rst | 8 ++++---- src/converse-core.js | 23 ++++++++++++++++------- src/converse-muc.js | 10 +++++++--- 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/docs/source/development.rst b/docs/source/development.rst index f30e1c8a0..7c33368ac 100644 --- a/docs/source/development.rst +++ b/docs/source/development.rst @@ -753,9 +753,9 @@ Here are the different events that are emitted: +---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+ | **contactRequest** | Someone has requested to subscribe to your presence (i.e. to be your contact). | ``converse.listen.on('contactRequest', function (event, user_data) { ... });`` | +---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+ -| **contactStatusChanged** | When a chat buddy's chat status has changed. | ``converse.listen.on('contactStatusChanged', function (event, buddy, status) { ... });`` | +| **contactStatusChanged** | When a chat buddy's chat status has changed. | ``converse.listen.on('contactStatusChanged', function (event, buddy) { ... });`` | +---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+ -| **contactStatusMessageChanged** | When a chat buddy's custom status message has changed. | ``converse.listen.on('contactStatusMessageChanged', function (event, buddy, messageText) { ... });`` | +| **contactStatusMessageChanged** | When a chat buddy's custom status message has changed. | ``converse.listen.on('contactStatusMessageChanged', function (event, data) { ... });`` | +---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+ | **message** | When a message is received. | ``converse.listen.on('message', function (event, messageXML) { ... });`` | +---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+ @@ -769,9 +769,9 @@ Here are the different events that are emitted: +---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+ | **reconnect** | After the connection has dropped. Converse.js will attempt to reconnect when not in prebind mode. | ``converse.listen.on('reconnect', function (event) { ... });`` | +---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+ -| **roomInviteSent** | After the user has sent out a direct invitation, to a roster contact, asking them to join a room. | ``converse.listen.on('roomInvite', function (event, roomview, invitee_jid, reason) { ... });`` | +| **roomInviteSent** | After the user has sent out a direct invitation, to a roster contact, asking them to join a room. | ``converse.listen.on('roomInvite', function (event, data) { ... });`` | +---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+ -| **roomInviteReceived** | After the user has sent out a direct invitation, to a roster contact, asking them to join a room. | ``converse.listen.on('roomInvite', function (event, roomview, invitee_jid, reason) { ... });`` | +| **roomInviteReceived** | After the user has sent out a direct invitation, to a roster contact, asking them to join a room. | ``converse.listen.on('roomInvite', function (event, data) { ... });`` | +---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+ | **roster** | When the roster is updated. | ``converse.listen.on('roster', function (event, items) { ... });`` | +---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+ diff --git a/src/converse-core.js b/src/converse-core.js index 32dfd1c75..5855258fd 100755 --- a/src/converse-core.js +++ b/src/converse-core.js @@ -54,21 +54,28 @@ interpolate : /\{\{([\s\S]+?)\}\}/g }; + // We create an object to act as the "this" context for event handlers (as + // defined below and accessible via converse_api.listen). + // We don't want the inner converse object to be the context, since it + // contains sensitive information, and we don't want it to be something in + // the DOM or window, because then anyone can trigger converse events. + var event_context = {}; + var converse = { plugins: {}, initialized_plugins: [], templates: templates, emit: function (evt, data) { - $(this).trigger(evt, data); + $(event_context).trigger(evt, data); }, once: function (evt, handler) { - $(this).one(evt, handler); + $(event_context).one(evt, handler); }, on: function (evt, handler) { - $(this).bind(evt, handler); + $(event_context).bind(evt, handler); }, off: function (evt, handler) { - $(this).unbind(evt, handler); + $(event_context).unbind(evt, handler); } }; @@ -2089,13 +2096,15 @@ this.$el.find('div.chat-event').remove(); } } - // FIXME: multiple parameters not accepted? - converse.emit('contactStatusChanged', item.attributes, item.get('chat_status')); + converse.emit('contactStatusChanged', item.attributes); }, onStatusChanged: function (item) { this.showStatusMessage(); - converse.emit('contactStatusMessageChanged', item.attributes, item.get('status')); + converse.emit('contactStatusMessageChanged', { + 'contact': item.attributes, + 'message': item.get('status') + }); }, onMinimizedChanged: function (item) { diff --git a/src/converse-muc.js b/src/converse-muc.js index 04870bffe..fb3b4af30 100755 --- a/src/converse-muc.js +++ b/src/converse-muc.js @@ -322,7 +322,7 @@ } }, - directInvite: function (receiver, reason) { + directInvite: function (recipient, reason) { var attrs = { xmlns: 'jabber:x:conference', jid: this.model.get('jid') @@ -331,11 +331,15 @@ if (this.model.get('password')) { attrs.password = this.model.get('password'); } var invitation = $msg({ from: converse.connection.jid, - to: receiver, + to: recipient, id: converse.connection.getUniqueId() }).c('x', attrs); converse.connection.send(invitation); - converse.emit('roomInviteSent', this, receiver, reason); + converse.emit('roomInviteSent', { + 'room': this, + 'recipient': recipient, + 'reason': reason + }); }, onCommandError: function (stanza) {