Fix incoming message display/storage

There were a few problems.

1. The message event was being triggered in background, not popup
2. The initial message/thread fetches from localStorage were mis-ordered
3. The timestamp wasn't being extracted from the right place
4. #3 caused messages to fail validation and not be saved

1-3 are fixed. To address 4 I switched validate() to log a warning
instead of preventing save.
This commit is contained in:
lilia 2014-06-03 19:37:10 -07:00
parent c90b9a5c59
commit a09a4776d3
3 changed files with 9 additions and 5 deletions

View file

@ -7,7 +7,7 @@ var Whisper = Whisper || {};
validate: function(attributes, options) {
var required = ['body', 'timestamp', 'threadId'];
var missing = _.filter(required, function(attr) { return !attributes[attr]; });
if (missing.length) { return "Message must have " + missing; }
if (missing.length) { console.log("Message missing attributes: " + missing); }
},
thread: function() {
@ -33,7 +33,7 @@ var Whisper = Whisper || {};
body: decrypted.message.body,
attachments: attachments,
type: 'incoming',
timestamp: decrypted.message.timestamp
timestamp: decrypted.pushMessage.timestamp
});
m.save();
@ -42,7 +42,6 @@ var Whisper = Whisper || {};
thread.set('unreadCount', thread.get('unreadCount') + 1);
thread.save();
}
thread.trigger('message', m);
return m;
},

View file

@ -34,6 +34,7 @@ var Whisper = Whisper || {};
this.listenTo(this.model, 'message', this.addMessage); // auto update
this.listenTo(this.model, 'destroy', this.remove); // auto update
this.listenTo(this.model, 'select', this.open);
this.listenTo(Whisper.Messages, 'reset', this.addAllMessages); // auto update
this.$el.addClass('closed');
this.$destroy = (new destroyer({model: this.model})).$el;
@ -51,7 +52,6 @@ var Whisper = Whisper || {};
this.$collapsable.append(this.$messages, this.$form);
this.$el.append(this.$destroy, this.$header, this.$collapsable);
this.addAllMessages();
this.$form.submit(function(input,thread){ return function(e) {
if (!input.val().length) { return false; }

View file

@ -14,13 +14,14 @@ var Whisper = Whisper || {};
this.listenTo(this.threads, 'add', this.addThread);
this.listenTo(this.threads, 'reset', this.addAll);
this.listenTo(this.threads, 'all', this.render);
this.listenTo(Whisper.Messages, 'add', this.addMessage);
// Suppresses 'add' events with {reset: true} and prevents the app view
// from being re-rendered for every model. Only renders when the 'reset'
// event is triggered at the end of the fetch.
//this.messages.threads({reset: true});
Whisper.Messages.fetch();
Whisper.Threads.fetch({reset: true});
Whisper.Messages.fetch();
this.$el.appendTo($('#inbox'));
@ -69,5 +70,9 @@ var Whisper = Whisper || {};
this.$el.html('');
this.threads.each(this.addThread, this);
},
addMessage: function(message) {
message.thread().trigger('message', message);
}
}))();
})();