Save incoming messages and pass to frontend asynchronously

After a message is saved asynchronsly, fire an event and pass the
message attributes to frontend listeners via the chrome-runtime API.

This behavior is similar to the 'storage' event fired by localStorage.
This commit is contained in:
lilia 2014-11-16 13:19:51 -08:00
parent ced295a630
commit 470346c9c4
9 changed files with 110 additions and 85 deletions

View file

@ -18,47 +18,50 @@ 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));
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.fileInput = new Whisper.FileInputView({
el: this.$el.find('.attachments')
});
this.fileInput = new Whisper.FileInputView({el: this.$el.find('.attachments')});
this.view = new Whisper.MessageListView({
collection: this.model.messages()
});
this.$el.find('.discussion-container').append(this.view.el);
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'
},
this.model.fetch({reset: true});
extension.onMessage('message', this.model.fetch.bind(this.model));
},
sendMessage: function(e) {
e.preventDefault();
var input = this.$el.find('.send input');
var message = input.val();
var convo = this.model;
events: {
'submit .send': 'sendMessage',
'close': 'remove'
},
if (message.length > 0 || this.fileInput.hasFiles()) {
this.fileInput.getFiles().then(function(attachments) {
convo.sendMessage(message, attachments);
});
input.val("");
}
},
sendMessage: function(e) {
e.preventDefault();
var input = this.$el.find('.send input');
var message = input.val();
var convo = this.model;
render: function() {
Whisper.Layout.setContent(this.$el.show());
this.view.scrollToBottom();
return this;
}
});
if (message.length > 0 || this.fileInput.hasFiles()) {
this.fileInput.getFiles().then(function(attachments) {
convo.sendMessage(message, attachments);
});
input.val("");
}
},
render: function() {
Whisper.Layout.setContent(this.$el.show());
this.view.scrollToBottom();
return this;
}
});
})();

View file

@ -18,7 +18,7 @@ var Whisper = Whisper || {};
this.collection.each(function(model) {
var view = new this.itemView({model: model});
this.$el.prepend(view.render().el);
});
}, this);
},
});
})();