Use separate message collections for each thread to facilitate lookup and lazy loading

This commit is contained in:
lilia 2014-07-22 04:14:33 -10:00
parent d6d17eaf19
commit ebf1b3352f
3 changed files with 18 additions and 7 deletions

View file

@ -15,6 +15,14 @@ var Whisper = Whisper || {};
}
});
Whisper.MessageCollection = Backbone.Collection.extend({
model: Message,
comparator: 'timestamp',
initialize: function(models, options) {
this.localStorage = new Backbone.LocalStorage("Messages-" + options.threadId);
}
});
Whisper.Messages = new (Backbone.Collection.extend({
localStorage: new Backbone.LocalStorage("Messages"),
model: Message,
@ -27,7 +35,7 @@ var Whisper = Whisper || {};
attachments[i] = "data:" + decrypted.message.attachments[i].contentType + ";base64," + btoa(getString(decrypted.message.attachments[i].decrypted));
var thread = Whisper.Threads.findOrCreateForIncomingMessage(decrypted);
var m = Whisper.Messages.add({
var m = thread.messages().add({
person: decrypted.pushMessage.source,
threadId: thread.id,
body: decrypted.message.body,
@ -38,15 +46,15 @@ var Whisper = Whisper || {};
m.save();
if (decrypted.message.timestamp > thread.get('timestamp')) {
thread.set('timestamp', decrypted.message.timestamp);
thread.set('unreadCount', thread.get('unreadCount') + 1);
thread.set({timestamp: decrypted.message.timestamp});
thread.set({unreadCount: thread.get('unreadCount') + 1});
thread.save();
}
return m;
},
addOutgoingMessage: function(message, thread) {
var m = Whisper.Messages.add({
var m = thread.messages().add({
threadId: thread.id,
body: message,
type: 'outgoing',