Browse Source

Add new event `pluginsInitialized` and use it in converse-notifications

By listening for this event before registering event handlers, other plugins
can first get their overrides of those handlers applied.
JC Brand 9 years ago
parent
commit
e12c165454
3 changed files with 24 additions and 5 deletions
  1. 13 0
      docs/source/development.rst
  2. 1 0
      src/converse-core.js
  3. 10 5
      src/converse-notification.js

+ 13 - 0
docs/source/development.rst

@@ -912,6 +912,8 @@ Once converse.js has been initialized.
 
 ``converse.listen.on('initialized', function (event) { ... });``
 
+See also `pluginsInitialized`_.
+
 messageSend
 ~~~~~~~~~~~
 
@@ -926,6 +928,17 @@ When keepalive=true but there aren't any stored prebind tokens.
 
 ``converse.listen.on('noResumeableSession', function (event) { ... });``
 
+pluginsInitialized
+~~~~~~~~~~~~~~~~~~
+
+Once all plugins have been initialized. This is a useful event if you want to
+register event handlers but would like your own handlers to be overridable by
+plugins. In that case, you need to first wait until all plugins have been
+initialized, so that their overrides are active. One example where this is used
+is in [converse-notifications.js](https://github.com/jcbrand/converse.js/blob/master/src/converse-notification.js).
+
+``converse.listen.on('pluginsInitialized', function (event) { ... });``
+
 reconnected
 ~~~~~~~~~~~
 

+ 1 - 0
src/converse-core.js

@@ -1786,6 +1786,7 @@
             'updateSettings': updateSettings,
             'converse': converse
         });
+        converse.emit('pluginsInitialized');
         converse._initialize();
         converse.registerGlobalEventHandlers();
         return init_deferred.promise();

+ 10 - 5
src/converse-notification.js

@@ -243,11 +243,16 @@
                 }
             };
 
-            converse.on('contactRequest',  converse.handleContactRequestNotification);
-            converse.on('contactStatusChanged',  converse.handleChatStateNotification);
-            converse.on('message',  converse.handleMessageNotification);
-            converse.on('feedback', converse.handleFeedback);
-            converse.on('connected', converse.requestPermission);
+            converse.on('pluginsInitialized', function () {
+                // We only register event handlers after all plugins are
+                // registered, because other plugins might override some of our
+                // handlers.
+                converse.on('contactRequest',  converse.handleContactRequestNotification);
+                converse.on('contactStatusChanged',  converse.handleChatStateNotification);
+                converse.on('message',  converse.handleMessageNotification);
+                converse.on('feedback', converse.handleFeedback);
+                converse.on('connected', converse.requestPermission);
+            });
         }
     });
 }));