From 1a30d003f5b55a3f50b5e43d6cf8902aa506a20b Mon Sep 17 00:00:00 2001 From: lilia Date: Sun, 13 Sep 2015 20:59:51 -0700 Subject: [PATCH] Fetch group contacts before fetching new messages Messages must wait for sender info to arrive before rendering. // FREEBIE --- js/models/conversations.js | 37 ++++++++++++++++++++++--------------- js/views/inbox_view.js | 7 ++++--- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/js/models/conversations.js b/js/models/conversations.js index e6c3663d..c9472da9 100644 --- a/js/models/conversations.js +++ b/js/models/conversations.js @@ -174,21 +174,28 @@ }, fetchContacts: function(options) { - if (this.isPrivate()) { - this.contactCollection.reset([this]); - } else { - var members = this.get('members') || []; - this.contactCollection.reset( - members.map(function(number) { - var c = ConversationController.create({ - id : number, - type : 'private' - }); - c.fetch(); - return c; - }.bind(this)) - ); - } + return new Promise(function(resolve) { + if (this.isPrivate()) { + this.contactCollection.reset([this]); + resolve(); + } else { + var promises = []; + var members = this.get('members') || []; + this.contactCollection.reset( + members.map(function(number) { + var c = ConversationController.create({ + id : number, + type : 'private' + }); + promises.push(new Promise(function(resolve) { + c.fetch().always(resolve); + })); + return c; + }.bind(this)) + ); + resolve(Promise.all(promises)); + } + }.bind(this)); }, reload: function() { diff --git a/js/views/inbox_view.js b/js/views/inbox_view.js index 9f648d5d..0724fc4d 100644 --- a/js/views/inbox_view.js +++ b/js/views/inbox_view.js @@ -54,7 +54,6 @@ appWindow: this.model.appWindow }); $el = view.$el; - conversation.fetchContacts(); if (conversation.messageCollection.length === 0) { $el.find('.message-list').addClass('loading'); } @@ -62,8 +61,10 @@ $el.prependTo(this.el); $el.find('.message-list').trigger('reset-scroll'); $el.trigger('force-resize'); - conversation.fetchMessages().then(function() { - $el.find('.message-list').removeClass('loading'); + conversation.fetchContacts().then(function() { + conversation.fetchMessages().then(function() { + $el.find('.message-list').removeClass('loading'); + }); }); conversation.markRead(); }