浏览代码

Add closure get/set methods for rosteritem views

JC Brand 11 年之前
父节点
当前提交
1567b212c3
共有 4 个文件被更改,包括 44 次插入18 次删除
  1. 11 6
      converse.js
  2. 1 1
      spec/chatbox.js
  3. 30 9
      spec/controlbox.js
  4. 2 2
      tests/utils.js

+ 11 - 6
converse.js

@@ -442,7 +442,7 @@
                 return converse.default_box_height;
                 return converse.default_box_height;
             }
             }
             var resistance = 10;
             var resistance = 10;
-            if ((height !== converse.default_box_height) && 
+            if ((height !== converse.default_box_height) &&
                 (Math.abs(height - converse.default_box_height) < resistance)) {
                 (Math.abs(height - converse.default_box_height) < resistance)) {
                 return converse.default_box_height;
                 return converse.default_box_height;
             }
             }
@@ -2925,9 +2925,14 @@
         this.RosterView = Backbone.View.extend({
         this.RosterView = Backbone.View.extend({
             tagName: 'dl',
             tagName: 'dl',
             id: 'converse-roster',
             id: 'converse-roster',
-            rosteritemviews: {},
 
 
             initialize: function () {
             initialize: function () {
+                var views = {};
+                this.get = function (id) {
+                    return views[id];
+                };
+                this.set = function (id, view) { views[id] = view; };
+
                 this.model.on("add", function (item) {
                 this.model.on("add", function (item) {
                     this.addRosterItemView(item).render(item);
                     this.addRosterItemView(item).render(item);
                     if (!item.get('vcard_updated')) {
                     if (!item.get('vcard_updated')) {
@@ -2983,15 +2988,15 @@
 
 
             addRosterItemView: function (item) {
             addRosterItemView: function (item) {
                 var view = new converse.RosterItemView({model: item});
                 var view = new converse.RosterItemView({model: item});
-                this.rosteritemviews[item.id] = view;
+                this.set(item.id, view);
                 return this;
                 return this;
             },
             },
 
 
             removeRosterItemView: function (item) {
             removeRosterItemView: function (item) {
-                var view = this.rosteritemviews[item.id];
+                var view = this.get(item.id);
                 if (view) {
                 if (view) {
                     view.$el.remove();
                     view.$el.remove();
-                    delete this.rosteritemviews[item.id];
+                    delete this.get(item.id);
                     this.render();
                     this.render();
                 }
                 }
                 return this;
                 return this;
@@ -3018,7 +3023,7 @@
                     $count, changed_presence;
                     $count, changed_presence;
                 if (item) {
                 if (item) {
                     var jid = item.id,
                     var jid = item.id,
-                        view = this.rosteritemviews[item.id],
+                        view = this.get(item.id),
                         ask = item.get('ask'),
                         ask = item.get('ask'),
                         subscription = item.get('subscription'),
                         subscription = item.get('subscription'),
                         requesting  = item.get('requesting'),
                         requesting  = item.get('requesting'),

+ 1 - 1
spec/chatbox.js

@@ -39,7 +39,7 @@
                 for (i=0; i<online_contacts.length; i++) {
                 for (i=0; i<online_contacts.length; i++) {
                     $el = $(online_contacts[i]);
                     $el = $(online_contacts[i]);
                     jid = $el.text().replace(' ','.').toLowerCase() + '@localhost';
                     jid = $el.text().replace(' ','.').toLowerCase() + '@localhost';
-                    view = this.rosterview.rosteritemviews[jid];
+                    view = this.rosterview.get(jid);
                     spyOn(view, 'openChat').andCallThrough();
                     spyOn(view, 'openChat').andCallThrough();
                     view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
                     view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
                     $el.click();
                     $el.click();

+ 30 - 9
spec/controlbox.js

@@ -9,6 +9,14 @@
 } (this, function (mock, utils) {
 } (this, function (mock, utils) {
     describe("The Control Box", $.proxy(function (mock, utils) {
     describe("The Control Box", $.proxy(function (mock, utils) {
 
 
+        beforeEach(function () {
+            runs(function () {
+                utils.openControlBox();
+            });
+            waits(250);
+            runs(function () {});
+        });
+
         it("can be opened by clicking a DOM element with class 'toggle-online-users'", $.proxy(function () {
         it("can be opened by clicking a DOM element with class 'toggle-online-users'", $.proxy(function () {
             runs(function () {
             runs(function () {
                 utils.closeControlBox();
                 utils.closeControlBox();
@@ -35,6 +43,15 @@
         }, converse));
         }, converse));
 
 
         describe("The Status Widget", $.proxy(function () {
         describe("The Status Widget", $.proxy(function () {
+
+            beforeEach(function () {
+                runs(function () {
+                    utils.openControlBox();
+                });
+                waits(250);
+                runs(function () {});
+            });
+
             it("shows the user's chat status, which is online by default", $.proxy(function () {
             it("shows the user's chat status, which is online by default", $.proxy(function () {
                 var view = this.xmppstatusview;
                 var view = this.xmppstatusview;
                 expect(view.$el.find('a.choose-xmpp-status').hasClass('online')).toBe(true);
                 expect(view.$el.find('a.choose-xmpp-status').hasClass('online')).toBe(true);
@@ -96,6 +113,9 @@
     }, converse, mock, utils));
     }, converse, mock, utils));
 
 
     describe("The Contacts Roster", $.proxy(function (mock, utils) {
     describe("The Contacts Roster", $.proxy(function (mock, utils) {
+        // FIXME: These tests are dependent on being run in order and cannot be
+        // run independently
+
         describe("Pending Contacts", $.proxy(function () {
         describe("Pending Contacts", $.proxy(function () {
             beforeEach(function () {
             beforeEach(function () {
                 runs(function () {
                 runs(function () {
@@ -134,7 +154,8 @@
             }, converse));
             }, converse));
 
 
             it("can be removed by the user", $.proxy(function () {
             it("can be removed by the user", $.proxy(function () {
-                var view = _.toArray(this.rosterview.rosteritemviews).pop();
+                var jid = mock.pend_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
+                var view = this.rosterview.get(jid);
                 spyOn(window, 'confirm').andReturn(true);
                 spyOn(window, 'confirm').andReturn(true);
                 spyOn(converse, 'emit');
                 spyOn(converse, 'emit');
                 spyOn(this.connection.roster, 'remove').andCallThrough();
                 spyOn(this.connection.roster, 'remove').andCallThrough();
@@ -234,7 +255,7 @@
                 spyOn(this.rosterview, 'render').andCallThrough();
                 spyOn(this.rosterview, 'render').andCallThrough();
                 for (i=0; i<3; i++) {
                 for (i=0; i<3; i++) {
                     jid = mock.cur_names[i].replace(' ','.').toLowerCase() + '@localhost';
                     jid = mock.cur_names[i].replace(' ','.').toLowerCase() + '@localhost';
-                    view = this.rosterview.rosteritemviews[jid];
+                    view = this.rosterview.get(jid);
                     spyOn(view, 'render').andCallThrough();
                     spyOn(view, 'render').andCallThrough();
                     item = view.model;
                     item = view.model;
                     item.set('chat_status', 'online');
                     item.set('chat_status', 'online');
@@ -253,7 +274,7 @@
                 spyOn(this.rosterview, 'render').andCallThrough();
                 spyOn(this.rosterview, 'render').andCallThrough();
                 for (i=3; i<6; i++) {
                 for (i=3; i<6; i++) {
                     jid = mock.cur_names[i].replace(' ','.').toLowerCase() + '@localhost';
                     jid = mock.cur_names[i].replace(' ','.').toLowerCase() + '@localhost';
-                    view = this.rosterview.rosteritemviews[jid];
+                    view = this.rosterview.get(jid);
                     spyOn(view, 'render').andCallThrough();
                     spyOn(view, 'render').andCallThrough();
                     item = view.model;
                     item = view.model;
                     item.set('chat_status', 'dnd');
                     item.set('chat_status', 'dnd');
@@ -272,7 +293,7 @@
                 spyOn(this.rosterview, 'render').andCallThrough();
                 spyOn(this.rosterview, 'render').andCallThrough();
                 for (i=6; i<9; i++) {
                 for (i=6; i<9; i++) {
                     jid = mock.cur_names[i].replace(' ','.').toLowerCase() + '@localhost';
                     jid = mock.cur_names[i].replace(' ','.').toLowerCase() + '@localhost';
-                    view = this.rosterview.rosteritemviews[jid];
+                    view = this.rosterview.get(jid);
                     spyOn(view, 'render').andCallThrough();
                     spyOn(view, 'render').andCallThrough();
                     item = view.model;
                     item = view.model;
                     item.set('chat_status', 'away');
                     item.set('chat_status', 'away');
@@ -291,7 +312,7 @@
                 spyOn(this.rosterview, 'render').andCallThrough();
                 spyOn(this.rosterview, 'render').andCallThrough();
                 for (i=9; i<12; i++) {
                 for (i=9; i<12; i++) {
                     jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
                     jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
-                    view = this.rosterview.rosteritemviews[jid];
+                    view = this.rosterview.get(jid);
                     spyOn(view, 'render').andCallThrough();
                     spyOn(view, 'render').andCallThrough();
                     item = view.model;
                     item = view.model;
                     item.set('chat_status', 'xa');
                     item.set('chat_status', 'xa');
@@ -310,7 +331,7 @@
                 spyOn(this.rosterview, 'render').andCallThrough();
                 spyOn(this.rosterview, 'render').andCallThrough();
                 for (i=12; i<15; i++) {
                 for (i=12; i<15; i++) {
                     jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
                     jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
-                    view = this.rosterview.rosteritemviews[jid];
+                    view = this.rosterview.get(jid);
                     spyOn(view, 'render').andCallThrough();
                     spyOn(view, 'render').andCallThrough();
                     item = view.model;
                     item = view.model;
                     item.set('chat_status', 'unavailable');
                     item.set('chat_status', 'unavailable');
@@ -388,7 +409,7 @@
                 // actually not accepted/authorized because of
                 // actually not accepted/authorized because of
                 // mock_connection.
                 // mock_connection.
                 var jid = mock.req_names.sort()[0].replace(' ','.').toLowerCase() + '@localhost';
                 var jid = mock.req_names.sort()[0].replace(' ','.').toLowerCase() + '@localhost';
-                var view = this.rosterview.rosteritemviews[jid];
+                var view = this.rosterview.get(jid);
                 spyOn(this.connection.roster, 'authorize');
                 spyOn(this.connection.roster, 'authorize');
                 spyOn(view, 'acceptRequest').andCallThrough();
                 spyOn(view, 'acceptRequest').andCallThrough();
                 view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
                 view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
@@ -400,7 +421,7 @@
 
 
             it("can have their requests denied by the user", $.proxy(function () {
             it("can have their requests denied by the user", $.proxy(function () {
                 var jid = mock.req_names.sort()[1].replace(/ /g,'.').toLowerCase() + '@localhost';
                 var jid = mock.req_names.sort()[1].replace(/ /g,'.').toLowerCase() + '@localhost';
-                var view = this.rosterview.rosteritemviews[jid];
+                var view = this.rosterview.get(jid);
                 spyOn(converse, 'emit');
                 spyOn(converse, 'emit');
                 spyOn(this.connection.roster, 'unauthorize');
                 spyOn(this.connection.roster, 'unauthorize');
                 spyOn(this.rosterview, 'removeRosterItemView').andCallThrough();
                 spyOn(this.rosterview, 'removeRosterItemView').andCallThrough();
@@ -451,7 +472,7 @@
                 // we make some online now
                 // we make some online now
                 for (i=0; i<5; i++) {
                 for (i=0; i<5; i++) {
                     jid = mock.cur_names[i].replace(' ','.').toLowerCase() + '@localhost';
                     jid = mock.cur_names[i].replace(' ','.').toLowerCase() + '@localhost';
-                    view = this.rosterview.rosteritemviews[jid];
+                    view = this.rosterview.get(jid);
                     view.model.set('chat_status', 'online');
                     view.model.set('chat_status', 'online');
                 }
                 }
             }, converse));
             }, converse));

+ 2 - 2
tests/utils.js

@@ -81,13 +81,13 @@
         var i = 0, jid, views = [];
         var i = 0, jid, views = [];
         for (i; i<amount; i++) {
         for (i; i<amount; i++) {
             jid = mock.cur_names[i].replace(' ','.').toLowerCase() + '@localhost';
             jid = mock.cur_names[i].replace(' ','.').toLowerCase() + '@localhost';
-            views[i] = converse.rosterview.rosteritemviews[jid].openChat(mock.event);
+            views[i] = converse.rosterview.get(jid).openChat(mock.event);
         }
         }
         return views;
         return views;
     };
     };
 
 
     utils.openChatBoxFor = function (jid) {
     utils.openChatBoxFor = function (jid) {
-        converse.rosterview.rosteritemviews[jid].openChat(mock.event);
+        converse.rosterview.get(jid).openChat(mock.event);
     };
     };
 
 
     utils.clearChatBoxMessages = function (jid) {
     utils.clearChatBoxMessages = function (jid) {