signal-desktop/js/views/conversation_view.js
lilia d362d0d978 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.
2014-10-24 14:49:42 -07:00

47 lines
1.3 KiB
JavaScript

var Whisper = Whisper || {};
(function () {
'use strict';
Whisper.ConversationView = Backbone.View.extend({
className: 'conversation',
initialize: function() {
this.listenTo(this.model, 'destroy', this.stopListening); // auto update
this.template = $('#conversation').html();
Mustache.parse(this.template);
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.model.messages().fetch();
}).bind(this));
},
events: {
'submit .send': 'sendMessage',
'close': 'remove'
},
sendMessage: function(e) {
e.preventDefault();
var input = this.$el.find('.send input');
if (input.val().length > 0) {
this.model.sendMessage(input.val());
input.val("");
}
},
scrollToBottom: function() {
this.view.$el.scrollTop(this.view.el.scrollHeight);
},
render: function() {
Whisper.Layout.setContent(this.$el.show());
this.scrollToBottom();
return this;
}
});
})();