Clean up the initialize method of RosterView

by moving event handers out
This commit is contained in:
JC Brand 2014-07-25 08:56:54 +02:00
parent 434e21d046
commit 0640dd5ae5
3 changed files with 37 additions and 28 deletions

View File

@ -645,8 +645,7 @@
}
});
this.Message = Backbone.Model.extend();
this.Message = Backbone.Model;
this.Messages = Backbone.Collection.extend({
model: converse.Message
});
@ -3238,29 +3237,11 @@
},
initialize: function () {
this.model.on("add", function (item) {
this.addRosterItemView(item).addRosterItem(item).updateRoster();
if (item.get('is_last')) {
this.sortRoster().showRoster();
}
if (!item.get('vcard_updated')) {
// This will update the vcard, which triggers a change
// request which will rerender the roster item.
converse.getVCard(item.get('jid'));
}
}, this);
this.model.on('change', function (item) {
if ((_.size(item.changed) === 1) && _.contains(_.keys(item.changed), 'sorted')) {
return;
}
this.updateChatBox(item).renderRosterItem(item).updateRoster();
if (item.changed.chat_status) { // A changed chat status implies a new sort order
this.sortRoster();
}
}, this);
this.model.on("remove", function (item) { this.removeRosterItemView(item); }, this);
this.model.on("destroy", function (item) { this.removeRosterItemView(item); }, this);
this.model.on("reset", function () { this.removeAllRosterItemViews(); }, this);
this.model.on("add", this.onAdd, this);
this.model.on('change', this.onChange, this);
this.model.on("remove", this.removeRosterItemView, this);
this.model.on("destroy", this.removeRosterItemView, this);
this.model.on("reset", this.removeAllRosterItemViews, this);
this.render();
this.model.fetch({add: true}); // Get the cached roster items from localstorage
},
@ -3288,6 +3269,28 @@
this.$el.hide().html(roster_markup);
},
onAdd: function (item) {
this.addRosterItemView(item).addRosterItem(item).updateRoster();
if (item.get('is_last')) {
this.sortRoster().showRoster();
}
if (!item.get('vcard_updated')) {
// This will update the vcard, which triggers a change
// request which will rerender the roster item.
converse.getVCard(item.get('jid'));
}
},
onChange: function (item) {
if ((_.size(item.changed) === 1) && _.contains(_.keys(item.changed), 'sorted')) {
return;
}
this.updateChatBox(item).renderRosterItem(item).updateRoster();
if (item.changed.chat_status) { // A changed chat status implies a new sort order
this.sortRoster();
}
},
updateChatBox: function (item, changed) {
var chatbox = converse.chatboxes.get(item.get('jid')),
changes = {};

View File

@ -226,7 +226,7 @@
allow_otr: true,
auto_list_rooms: false,
auto_subscribe: false,
bosh_service_url: 'http://192.168.56.2:8890/http-bind', // Please use this connection manager only for testing purposes
bosh_service_url: 'http://conversejs.containers:5280/http-bind', // Please use this connection manager only for testing purposes
debug: true ,
hide_muc_server: false,
i18n: locales['en'], // Refer to ./locale/locales.js to see which locales are supported

View File

@ -440,6 +440,7 @@
describe("Requesting Contacts", $.proxy(function () {
beforeEach($.proxy(function () {
runs(function () {
utils.clearBrowserStorage();
converse.rosterview.model.reset();
utils.createContacts('requesting').openControlBox();
});
@ -510,14 +511,19 @@
}, converse));
it("can have their requests denied by the user", $.proxy(function () {
var jid = mock.req_names.sort()[1].replace(/ /g,'.').toLowerCase() + '@localhost';
var view = this.rosterview.get(jid);
this.rosterview.model.reset();
spyOn(converse, 'emit');
spyOn(this.connection.roster, 'unauthorize');
spyOn(this.rosterview, 'removeRosterItemView').andCallThrough();
spyOn(window, 'confirm').andReturn(true);
this.rosterview.initialize(); // Must be initialized only after the spy has been called
utils.createContacts('requesting').openControlBox();
var jid = mock.req_names.sort()[1].replace(/ /g,'.').toLowerCase() + '@localhost';
var view = this.rosterview.get(jid);
spyOn(view, 'declineRequest').andCallThrough();
view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
var accept_button = view.$el.find('.decline-xmpp-request');
accept_button.click();
expect(view.declineRequest).toHaveBeenCalled();