Move roster change listener to a seprate method.

Move render, show and message fetching from ChatBoxesViews to ChatBoxView
This commit is contained in:
JC Brand 2013-03-24 11:48:12 +02:00
parent 1e3427adcf
commit 75cb595d50

View File

@ -448,49 +448,46 @@
}, },
closeChat: function () { closeChat: function () {
this.model.set({'visible': false}) this.model.destroy();
},
rosterChanged: function () {
// FIXME: This event handler should go onto the roster itself, then it
// will be called once (for the roster) and not once per open
// chatbox
var fullname = this.model.get('fullname'),
chat_status = item.get('chat_status');
if (item.get('jid') === this.model.get('jid')) {
if (_.has(changed.changes, 'chat_status')) {
if (this.$el.is(':visible')) {
if (chat_status === 'offline') {
this.insertStatusNotification(fullname+' '+'has gone offline');
} else if (chat_status === 'away') {
this.insertStatusNotification(fullname+' '+'has gone away');
} else if ((chat_status === 'dnd')) {
this.insertStatusNotification(fullname+' '+'is busy');
} else if (chat_status === 'online') {
this.$el.find('div.chat-event').remove();
}
}
} else if (_.has(changed.changes, 'status')) {
this.$el.find('p.user-custom-message').text(item.get('status')).attr('title', item.get('status'));
}
}
}, },
initialize: function (){ initialize: function (){
// boxviewinit
$('body').append(this.$el.hide()); $('body').append(this.$el.hide());
this.model.messages.on('add', function (item) { this.model.messages.on('add', this.messageReceived, this);
this.messageReceived(item);
}, this);
this.model.on('change', $.proxy(function (item, changed) { this.model.on('destroy', $.proxy(function (model, response, options) {
if (_.has(item.changed, 'visible')) { this.$el.hide('fast');
if (item.changed['visible'] === true) {
this.show();
} else {
this.$el.hide('fast', function () {
this.remove();
item.destroy();
});
}
}
}, this)); }, this));
xmppchat.roster.on('change', function (item, changed) { xmppchat.roster.on('change', this.rosterChanged, this);
var fullname = this.model.get('fullname'),
chat_status = item.get('chat_status'); this.render().show().model.messages.fetch({add: true});
if (item.get('jid') === this.model.get('jid')) {
if (_.has(changed.changes, 'chat_status')) {
if (this.$el.is(':visible')) {
if (chat_status === 'offline') {
this.insertStatusNotification(fullname+' '+'has gone offline');
} else if (chat_status === 'away') {
this.insertStatusNotification(fullname+' '+'has gone away');
} else if ((chat_status === 'dnd')) {
this.insertStatusNotification(fullname+' '+'is busy');
} else if (chat_status === 'online') {
this.$el.find('div.chat-event').remove();
}
}
} else if (_.has(changed.changes, 'status')) {
this.$el.find('p.user-custom-message').text(item.get('status')).attr('title', item.get('status'));
}
}
}, this);
}, },
template: _.template( template: _.template(
@ -731,14 +728,13 @@
if (_.has(item.changed, 'visible')) { if (_.has(item.changed, 'visible')) {
if (item.changed['visible'] === true) { if (item.changed['visible'] === true) {
this.show(); this.show();
} else {
this.$el.hide('fast', function () {
this.remove();
item.destroy();
});
} }
} }
}, this)); }, this));
this.model.on('destroy', $.proxy(function (model, response, options) {
this.$el.hide('fast');
}, this));
}, },
setUpRoster: function () { setUpRoster: function () {
@ -1047,7 +1043,6 @@
add: true, success: add: true, success:
$.proxy(function (collection, resp) { $.proxy(function (collection, resp) {
if (_.include(_.pluck(resp, 'id'), 'controlbox')) { if (_.include(_.pluck(resp, 'id'), 'controlbox')) {
// FIXME:
// If the controlbox was saved in localstorage, it must be visible // If the controlbox was saved in localstorage, it must be visible
this.get('controlbox').set({visible:true}) this.get('controlbox').set({visible:true})
} }
@ -1089,13 +1084,6 @@
return model; return model;
}, },
closeChat: function (jid) {
var view = this.views[jid];
if (view) {
view.closeChat();
}
},
openControlBox: function () { openControlBox: function () {
if (this.model.get('controlbox')) { if (this.model.get('controlbox')) {
this.showChat('controlbox'); this.showChat('controlbox');
@ -1192,23 +1180,17 @@
}, },
initialize: function () { initialize: function () {
// boxesviewinit
this.views = {}; this.views = {};
this.options.model.on("add", function (item) { this.options.model.on("add", function (item) {
var view; var view;
if (item.get('box_id') === 'controlbox') { if (item.get('box_id') === 'controlbox') {
view = new xmppchat.ControlBoxView({model: item}); view = new xmppchat.ControlBoxView({model: item});
this.views['controlbox'] = view.render();
view.$el.appendTo(this.$el);
} 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();
view.$el.appendTo(this.$el);
view.model.messages.fetch({add: true});
if (item.get('visible')) {
this.showChat(item.get('id'));
}
} }
this.views[item.get('id')] = view;
view.$el.appendTo(this.$el);
}, this); }, this);
} }
}); });
@ -2036,7 +2018,7 @@
$toggle.bind('click', $.proxy(function (e) { $toggle.bind('click', $.proxy(function (e) {
e.preventDefault(); e.preventDefault();
if ($("div#controlbox").is(':visible')) { if ($("div#controlbox").is(':visible')) {
this.chatboxesview.closeChat('controlbox'); this.chatboxes.get('controlbox').destroy();
} else { } else {
this.chatboxesview.openControlBox(); this.chatboxesview.openControlBox();
} }