Browse Source

Replace jQuery-based event emitter with Backbone.Events

JC Brand 8 years ago
parent
commit
647ee1ff04

+ 3 - 1
docs/CHANGES.md

@@ -5,8 +5,10 @@
 - Case insensitive matching of moderation commands. [jcbrand]
 - Add `/subject` as alias to `/topic` [jcbrand]
 - `allow_chat_pending_contacts` now defaults to `true` [jcbrand]
+- Breaking change: Callbacks for `converse.on` now no longer receive an event
+  object as first parameter. [jcbrand]
 
-## 2.0.5 (2017-02-01)
+## 2.0.5 (Unreleased)
 - #743, #751, #753 Update to Strophe 1.2.12. SASL-EXTERNAL now has reduced priority, so it won't
   be prioritized above other auth mechanisms. [jcbrand]
 - #755: create composer.json to add this project in packagist.org [fabiomontefuscolo]

+ 1 - 1
docs/source/configuration.rst

@@ -1059,7 +1059,7 @@ Allows you to show or hide buttons on the chat boxes' toolbars.
     Provides a button with a picture of a telephone on it.
     When the call button is pressed, it will emit an event that can be used by a third-party library to initiate a call.::
 
-        converse.listen.on('callButtonClicked', function(event, data) {
+        converse.listen.on('callButtonClicked', function(data) {
             console.log('Strophe connection is', data.connection);
             console.log('Bare buddy JID is', data.model.get('jid'));
             // ... Third-party library code ...

+ 4 - 4
docs/source/developer_api.rst

@@ -151,7 +151,7 @@ For example:
 
 .. code-block:: javascript
 
-    converse.listen.on('serviceDiscovered', function (event, feature) {
+    converse.listen.on('serviceDiscovered', function (feature) {
         if (feature.get('var') === converse.env.Strophe.NS.MAM) {
             converse.archive.query()
         }
@@ -687,7 +687,7 @@ grouping:
 
 .. code-block:: javascript
 
-        converse.listen.on('message', function (event, messageXML) { ... });
+        converse.listen.on('message', function (messageXML) { ... });
 
 * **once(eventName, callback, [context])**:
 
@@ -704,7 +704,7 @@ grouping:
 
 .. code-block:: javascript
 
-        converse.listen.once('message', function (event, messageXML) { ... });
+        converse.listen.once('message', function (messageXML) { ... });
 
 * **not(eventName, callback)**
 
@@ -719,5 +719,5 @@ grouping:
 
 .. code-block:: javascript
 
-        converse.listen.not('message', function (event, messageXML) { ... });
+        converse.listen.not('message', function (messageXML) { ... });
 

+ 3 - 9
src/converse-api.js

@@ -161,15 +161,9 @@
             }
         },
         'listen': {
-            'once': function (evt, handler, context) {
-                converse.once(evt, handler, context);
-            },
-            'on': function (evt, handler, context) {
-                converse.on(evt, handler, context);
-            },
-            'not': function (evt, handler) {
-                converse.off(evt, handler);
-            },
+            'once': converse.once,
+            'on': converse.on,
+            'not': converse.off,
             'stanza': function (name, options, handler) {
                 if (_.isFunction(options)) {
                     handler = options;

+ 9 - 37
src/converse-core.js

@@ -43,41 +43,10 @@
         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 = {
-        templates: {},
-
-        emit: function (evt, data) {
-            $(event_context).trigger(evt, data);
-        },
-
-        once: function (evt, handler, context) {
-            if (context) {
-                handler = handler.bind(context);
-            }
-            $(event_context).one(evt, handler);
-        },
-
-        on: function (evt, handler, context) {
-            if (_.includes(['ready', 'initialized'], evt)) {
-                converse.log('Warning: The "'+evt+'" event has been deprecated and will be removed, please use "connected".');
-            }
-            if (context) {
-                handler = handler.bind(context);
-            }
-            $(event_context).bind(evt, handler);
-        },
-
-        off: function (evt, handler) {
-            $(event_context).unbind(evt, handler);
-        }
-    };
+    var converse = {};
+    converse.templates = {};
+    _.extend(converse, Backbone.Events);
+    converse.emit = converse.trigger;
 
     // Make converse pluggable
     pluggable.enable(converse, 'converse', 'pluggable');
@@ -142,9 +111,12 @@
         if (!_.isUndefined(converse.chatboxes)) {
             // Looks like converse.initialized was called again without logging
             // out or disconnecting in the previous session.
-            // This happens in tests.
-            // We therefore first clean up.
+            // This happens in tests. We therefore first clean up.
             converse.connection.reset();
+            // TODO: Looks like the next two lines might have to go into
+            // _tearDown or some place similar.
+            converse.off();
+            converse.stopListening();
             converse._tearDown();
         }
 

+ 1 - 1
src/converse-mam.js

@@ -278,7 +278,7 @@
             };
 
 
-            var onFeatureAdded = function (evt, feature) {
+            var onFeatureAdded = function (feature) {
                 var prefs = feature.get('preferences') || {};
                 if (feature.get('var') === Strophe.NS.MAM && prefs['default'] !== converse.message_archiving) {
                     // Ask the server for archiving preferences

+ 2 - 2
src/converse-minimize.js

@@ -504,7 +504,7 @@
                 }
             });
 
-            var renderMinimizeButton = function (evt, view) {
+            var renderMinimizeButton = function (view) {
                 // Inserts a "minimize" button in the chatview's header
                 var $el = view.$el.find('.toggle-chatbox-button');
                 var $new_el = converse.templates.chatbox_minimize(
@@ -518,7 +518,7 @@
             };
             converse.on('chatBoxOpened', renderMinimizeButton);
 
-            converse.on('controlBoxOpened', function (evt, chatbox) {
+            converse.on('controlBoxOpened', function (chatbox) {
                 // Wrapped in anon method because at scan time, chatboxviews
                 // attr not set yet.
                 if (converse.connection.connected) {

+ 5 - 5
src/converse-notification.js

@@ -205,7 +205,7 @@
                 }
             };
 
-            converse.handleChatStateNotification = function (evt, contact) {
+            converse.handleChatStateNotification = function (contact) {
                 /* Event handler for on('contactStatusChanged').
                  * Will show an HTML5 notification to indicate that the chat
                  * status has changed.
@@ -215,7 +215,7 @@
                 }
             };
 
-            converse.handleMessageNotification = function (evt, message) {
+            converse.handleMessageNotification = function (message) {
                 /* Event handler for the on('message') event. Will call methods
                  * to play sounds and show HTML5 notifications.
                  */
@@ -229,19 +229,19 @@
                 }
             };
 
-            converse.handleContactRequestNotification = function (evt, contact) {
+            converse.handleContactRequestNotification = function (contact) {
                 if (converse.areDesktopNotificationsEnabled(true)) {
                     converse.showContactRequestNotification(contact);
                 }
             };
 
-            converse.handleFeedback = function (evt, data) {
+            converse.handleFeedback = function (data) {
                 if (converse.areDesktopNotificationsEnabled(true)) {
                     converse.showFeedbackNotification(data);
                 }
             };
 
-            converse.requestPermission = function (evt) {
+            converse.requestPermission = function () {
                 if (converse.supports_html5_notification &&
                     ! _.includes(['denied', 'granted'], Notification.permission)) {
                     // Ask user to enable HTML5 notifications

+ 1 - 1
src/converse-vcard.js

@@ -132,7 +132,7 @@
                 }
             };
 
-            var updateVCardForChatBox = function (evt, chatbox) {
+            var updateVCardForChatBox = function (chatbox) {
                 if (!converse.use_vcards) { return; }
                 var jid = chatbox.model.get('jid'),
                     contact = converse.roster.get(jid);