Browse Source

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.
JC Brand 9 năm trước cách đây
mục cha
commit
23a1dc4f2b
3 tập tin đã thay đổi với 27 bổ sung14 xóa
  1. 4 4
      docs/source/development.rst
  2. 16 7
      src/converse-core.js
  3. 7 3
      src/converse-muc.js

+ 4 - 4
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) { ... });``                                   |
 +---------------------------------+---------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------+

+ 16 - 7
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) {

+ 7 - 3
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) {