From d362d0d9785c99982f0a5f7f98fa07719334b45b Mon Sep 17 00:00:00 2001 From: lilia Date: Fri, 24 Oct 2014 14:03:34 -0700 Subject: [PATCH] Autoscroll conversation views Scroll to the bottom (most recent) message in the conversation when it is opened, when we send a message, and when we receive a message. --- js/views/conversation_view.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/js/views/conversation_view.js b/js/views/conversation_view.js index fc7189d5..9dd5bd7b 100644 --- a/js/views/conversation_view.js +++ b/js/views/conversation_view.js @@ -12,10 +12,12 @@ var Whisper = Whisper || {}; this.$el.html(Mustache.render(this.template)); this.view = new Whisper.MessageListView({collection: this.model.messages()}); + this.listenTo(this.model.messages(), 'add', this.scrollToBottom); + this.model.messages().fetch({reset: true}); this.$el.find('.discussion-container').append(this.view.el); window.addEventListener('storage', (function(){ - this.fetch(); + this.model.messages().fetch(); }).bind(this)); }, events: { @@ -23,10 +25,6 @@ var Whisper = Whisper || {}; 'close': 'remove' }, - fetch: function() { - this.model.messages().fetch({reset: true}); - }, - sendMessage: function(e) { e.preventDefault(); var input = this.$el.find('.send input'); @@ -36,8 +34,13 @@ var Whisper = Whisper || {}; } }, + scrollToBottom: function() { + this.view.$el.scrollTop(this.view.el.scrollHeight); + }, + render: function() { Whisper.Layout.setContent(this.$el.show()); + this.scrollToBottom(); return this; } });