Messages are now also saved and retrieved via backbone.localStorage

This commit is contained in:
JC Brand 2013-03-22 15:02:33 +02:00
parent b4aada0155
commit 6fe5ac7224

View File

@ -267,7 +267,6 @@
this.messages = new xmppchat.Messages(); this.messages = new xmppchat.Messages();
this.messages.localStorage = new Backbone.LocalStorage( this.messages.localStorage = new Backbone.LocalStorage(
hex_sha1('converse.messages'+this.get('jid'))); hex_sha1('converse.messages'+this.get('jid')));
this.set({ this.set({
'user_id' : Strophe.getNodeFromJid(this.get('jid')), 'user_id' : Strophe.getNodeFromJid(this.get('jid')),
'box_id' : hex_sha1(this.get('jid')), 'box_id' : hex_sha1(this.get('jid')),
@ -449,18 +448,24 @@
var message = $msg({from: xmppchat.connection.bare_jid, to: bare_jid, type: 'chat', id: timestamp}) var message = $msg({from: xmppchat.connection.bare_jid, to: bare_jid, type: 'chat', id: timestamp})
.c('body').t(text).up() .c('body').t(text).up()
.c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}); .c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'});
// Forward the message, so that other connected resources are also aware of it. // Forward the message, so that other connected resources are also aware of it.
// TODO: Forward the message only to other connected resources (inside the browser) // TODO: Forward the message only to other connected resources (inside the browser)
var forwarded = $msg({to:xmppchat.connection.bare_jid, type:'chat', id:timestamp}) var forwarded = $msg({to:xmppchat.connection.bare_jid, type:'chat', id:timestamp})
.c('forwarded', {xmlns:'urn:xmpp:forward:0'}) .c('forwarded', {xmlns:'urn:xmpp:forward:0'})
.c('delay', {xmns:'urn:xmpp:delay',stamp:timestamp}).up() .c('delay', {xmns:'urn:xmpp:delay',stamp:timestamp}).up()
.cnode(message.tree()); .cnode(message.tree());
xmppchat.connection.send(message); xmppchat.connection.send(message);
xmppchat.connection.send(forwarded); xmppchat.connection.send(forwarded);
this.appendMessage(text);
// xmppchat.storage.addMessage(bare_jid, text, 'to'); // Add the new message
var message = new xmppchat.Message({
fullname: 'me',
sender: 'me',
time: (new Date()).toLocaleTimeString().substring(0,5),
message: text
});
this.model.messages.add(message);
message.save();
}, },
keyPressed: function (ev) { keyPressed: function (ev) {
@ -470,6 +475,7 @@
composing; composing;
if(ev.keyCode == 13) { if(ev.keyCode == 13) {
ev.preventDefault();
message = $textarea.val(); message = $textarea.val();
$textarea.val('').focus(); $textarea.val('').focus();
if (message !== '') { if (message !== '') {
@ -1206,8 +1212,10 @@
xmppchat.rosterview.initialSort(); xmppchat.rosterview.initialSort();
} else { } else {
view = new xmppchat.ChatBoxView({model: item}); view = new xmppchat.ChatBoxView({model: item});
// Fetch messages from localstorage
this.views[item.get('id')] = view.render(); this.views[item.get('id')] = view.render();
view.$el.appendTo(this.$el); view.$el.appendTo(this.$el);
view.model.messages.fetch({add: true});
this.showChat(item.get('id')); this.showChat(item.get('id'));
} }
}, this); }, this);
@ -2073,6 +2081,7 @@
this.chatboxes.localStorage = new Backbone.LocalStorage( this.chatboxes.localStorage = new Backbone.LocalStorage(
hex_sha1('converse.chatboxes-'+xmppchat.connection.bare_jid)); hex_sha1('converse.chatboxes-'+xmppchat.connection.bare_jid));
this.xmppstatus = new this.XMPPStatus();
this.chatboxesview.onConnected(); this.chatboxesview.onConnected();
this.connection.addHandler( this.connection.addHandler(
@ -2098,7 +2107,6 @@
}, this), null, 'message', 'chat'); }, this), null, 'message', 'chat');
// XMPP Status // XMPP Status
this.xmppstatus = new this.XMPPStatus();
this.xmppstatusview = new this.XMPPStatusView({ this.xmppstatusview = new this.XMPPStatusView({
'model': this.xmppstatus 'model': this.xmppstatus
}); });