瀏覽代碼

Let core populated the roster, and send the initial presence

This allows for cleaner separation between core and rosterview, making it
easier to remove that plugin for more stripped down deployments.
JC Brand 8 年之前
父節點
當前提交
ab76f1da44
共有 4 個文件被更改,包括 46 次插入29 次删除
  1. 2 0
      docs/CHANGES.md
  2. 13 0
      docs/source/development.rst
  3. 14 0
      src/converse-core.js
  4. 17 29
      src/converse-rosterview.js

+ 2 - 0
docs/CHANGES.md

@@ -3,6 +3,8 @@
 ## 2.0.1 (Unreleased)
 ## 2.0.1 (Unreleased)
 - Allow the context (i.e. `this` value) to be passed in when registering event
 - Allow the context (i.e. `this` value) to be passed in when registering event
   listeners with `converse.listen.on` and `converse.listen.once`. [jcbrand]
   listeners with `converse.listen.on` and `converse.listen.once`. [jcbrand]
+- New event ['rosterContactsFetched'](https://conversejs.org/docs/html/development.html#rosterContactsFetched) [jcbrand]
+- New event ['rosterGroupsFetched'](https://conversejs.org/docs/html/development.html#rosterGroupsFetched) [jcbrand]
 
 
 ## 2.0.0 (2016-09-16)
 ## 2.0.0 (2016-09-16)
 - #656 Online users count not shown initially [amanzur]
 - #656 Online users count not shown initially [amanzur]

+ 13 - 0
docs/source/development.rst

@@ -996,6 +996,19 @@ When the roster has been received from the XMPP server.
 See also the `cachedRoster` event further up, which gets called instead of
 See also the `cachedRoster` event further up, which gets called instead of
 `roster` if its already in `sessionStorage`.
 `roster` if its already in `sessionStorage`.
 
 
+rosterContactsFetched
+~~~~~~~~~~~~~~~~~~~~~
+
+Triggered once roster contacts have been fetched. Used by the
+`converse-rosterview.js` plugin to know when it can start to show the roster.
+
+rosterGroupsFetched
+~~~~~~~~~~~~~~~~~~~
+
+Triggered once roster groups have been fetched. Used by the
+`converse-rosterview.js` plugin to know when it can start alphabetically
+position roster groups.
+
 rosterPush
 rosterPush
 ~~~~~~~~~~
 ~~~~~~~~~~
 
 

+ 14 - 0
src/converse-core.js

@@ -667,6 +667,19 @@
                 b64_sha1('converse.roster.groups'+converse.bare_jid));
                 b64_sha1('converse.roster.groups'+converse.bare_jid));
         };
         };
 
 
+        this.populateRoster = function () {
+            /* Fetch all the roster groups, and then the roster contacts.
+             * Emit an event after fetching is done in each case.
+             */
+            converse.rostergroups.fetchRosterGroups().then(function () {
+                converse.emit('rosterGroupsFetched');
+                converse.roster.fetchRosterContacts().then(function () {
+                    converse.emit('rosterContactsFetched');
+                    converse.sendInitialPresence();
+                });
+            });
+        };
+
         this.unregisterPresenceHandler = function () {
         this.unregisterPresenceHandler = function () {
             if (typeof converse.presence_ref !== 'undefined') {
             if (typeof converse.presence_ref !== 'undefined') {
                 converse.connection.deleteHandler(converse.presence_ref);
                 converse.connection.deleteHandler(converse.presence_ref);
@@ -693,6 +706,7 @@
         this.onStatusInitialized = function () {
         this.onStatusInitialized = function () {
             this.registerIntervalHandler();
             this.registerIntervalHandler();
             this.initRoster();
             this.initRoster();
+            this.populateRoster();
             this.chatboxes.onConnected();
             this.chatboxes.onConnected();
             this.registerPresenceHandler();
             this.registerPresenceHandler();
             this.giveFeedback(__('Contacts'));
             this.giveFeedback(__('Contacts'));

+ 17 - 29
src/converse-rosterview.js

@@ -39,10 +39,7 @@
                 converse.rosterview = new converse.RosterView({
                 converse.rosterview = new converse.RosterView({
                     'model': converse.rostergroups
                     'model': converse.rostergroups
                 });
                 });
-                converse.rosterview.render().populate().then(function () {
-                    converse.rosterview.update();
-                    converse.sendInitialPresence();
-                });
+                converse.rosterview.render();
             },
             },
 
 
             RosterGroups: {
             RosterGroups: {
@@ -248,17 +245,13 @@
                     converse.roster.on("remove", this.update, this);
                     converse.roster.on("remove", this.update, this);
                     this.model.on("add", this.onGroupAdd, this);
                     this.model.on("add", this.onGroupAdd, this);
                     this.model.on("reset", this.reset, this);
                     this.model.on("reset", this.reset, this);
-                    this.$roster = $('<dl class="roster-contacts" style="display: none;"></dl>');
-                    // Create a model on which we can store filter properties
-                    var model = new converse.RosterFilter();
-                    model.id = b64_sha1('converse.rosterfilter'+converse.bare_jid);
-                    model.browserStorage = new Backbone.BrowserStorage.local(this.filter.id);
-                    this.filter_view = new converse.RosterFilterView({'model': model});
-                    this.filter_view.model.on('change', this.updateFilter, this);
-                    this.filter_view.model.fetch();
+                    converse.on('rosterGroupsFetched', this.positionFetchedGroups, this);
+                    converse.on('rosterContactsFetched', this.update, this);
+                    this.createRosterFilter();
                 },
                 },
 
 
                 render: function () {
                 render: function () {
+                    this.$roster = $('<dl class="roster-contacts" style="display: none;"></dl>');
                     this.$el.html(this.filter_view.render());
                     this.$el.html(this.filter_view.render());
                     if (!converse.allow_contact_requests) {
                     if (!converse.allow_contact_requests) {
                         // XXX: if we ever support live editing of config then
                         // XXX: if we ever support live editing of config then
@@ -268,6 +261,16 @@
                     return this;
                     return this;
                 },
                 },
 
 
+                createRosterFilter: function () {
+                    // Create a model on which we can store filter properties
+                    var model = new converse.RosterFilter();
+                    model.id = b64_sha1('converse.rosterfilter'+converse.bare_jid);
+                    model.browserStorage = new Backbone.BrowserStorage.local(this.filter.id);
+                    this.filter_view = new converse.RosterFilterView({'model': model});
+                    this.filter_view.model.on('change', this.updateFilter, this);
+                    this.filter_view.model.fetch();
+                },
+
                 updateFilter: _.debounce(function () {
                 updateFilter: _.debounce(function () {
                     /* Filter the roster again.
                     /* Filter the roster again.
                      * Called whenever the filter settings have been changed or
                      * Called whenever the filter settings have been changed or
@@ -310,18 +313,6 @@
                     return this;
                     return this;
                 },
                 },
 
 
-                populate: function () {
-                    /* Fetch the roster groups, position them and then fetch
-                     * the roster contacts.
-                     */
-                    var deferred = new $.Deferred();
-                    this.model.fetchRosterGroups().then(function () {
-                        this.positionFetchedGroups.apply(this, arguments);
-                        converse.roster.fetchRosterContacts().then(deferred.resolve);
-                    }.bind(this));
-                    return deferred.promise();
-                },
-
                 filter: function (query, type) {
                 filter: function (query, type) {
                     // First we make sure the filter is restored to its
                     // First we make sure the filter is restored to its
                     // original state
                     // original state
@@ -435,11 +426,8 @@
                      * positioned aren't already in inserted into the
                      * positioned aren't already in inserted into the
                      * roster DOM element.
                      * roster DOM element.
                      */
                      */
-                    if (model.length === 0) {
-                        return;
-                    }
-                    model.sort();
-                    model.each(function (group, idx) {
+                    this.model.sort();
+                    this.model.each(function (group, idx) {
                         var view = this.get(group.get('name'));
                         var view = this.get(group.get('name'));
                         if (!view) {
                         if (!view) {
                             view = new converse.RosterGroupView({model: group});
                             view = new converse.RosterGroupView({model: group});