From fd3a72d4350d4a44b03bd35611fc72e56cf261ff Mon Sep 17 00:00:00 2001 From: lilia Date: Sun, 16 Nov 2014 16:01:28 -0800 Subject: [PATCH] Destroy all globals Well, not *all* globals.. --- js/models/conversations.js | 2 -- js/models/messages.js | 4 ---- js/views/conversation_list_view.js | 1 - js/views/inbox_view.js | 38 ++++++++++++++++++------------ js/views/new_conversation_view.js | 10 ++------ js/views/new_group_view.js | 10 ++------ test/views/message_view_test.js | 5 ++-- 7 files changed, 30 insertions(+), 40 deletions(-) diff --git a/js/models/conversations.js b/js/models/conversations.js index f65adebc..4cc82d6b 100644 --- a/js/models/conversations.js +++ b/js/models/conversations.js @@ -181,6 +181,4 @@ })); } }); - - Whisper.Conversations = new Whisper.ConversationCollection(); })(); diff --git a/js/models/messages.js b/js/models/messages.js index 353992b3..a4b2b77a 100644 --- a/js/models/messages.js +++ b/js/models/messages.js @@ -28,10 +28,6 @@ if (missing.length) { console.log("Message missing attributes: " + missing); } - }, - - conversation: function() { - return Whisper.Conversations.get(this.get('conversationId')); } }); diff --git a/js/views/conversation_list_view.js b/js/views/conversation_list_view.js index 3a398f8e..3c5f9d34 100644 --- a/js/views/conversation_list_view.js +++ b/js/views/conversation_list_view.js @@ -7,7 +7,6 @@ var Whisper = Whisper || {}; tagName: 'div', id: 'contacts', itemView: Whisper.ConversationListItemView, - collection: Whisper.Conversations, events: { 'click .contact': 'select', diff --git a/js/views/inbox_view.js b/js/views/inbox_view.js index dafeed36..b30b2526 100644 --- a/js/views/inbox_view.js +++ b/js/views/inbox_view.js @@ -14,9 +14,9 @@ * along with this program. If not, see . */ (function () { - 'use strict'; + 'use strict'; - window.Whisper = window.Whisper || {}; + window.Whisper = window.Whisper || {}; Whisper.InboxView = Backbone.View.extend({ initialize: function () { @@ -27,16 +27,21 @@ window.addEventListener('resize', this.resize.bind(this)); this.conversations = new Whisper.ConversationCollection(); - new Whisper.ConversationListView({el: $('#contacts'), collection: Whisper.Conversations}); - Whisper.Conversations.fetch({reset: true}).then(function() { - if (Whisper.Conversations.length) { - Whisper.Conversations.at(0).trigger('render'); - } + new Whisper.ConversationListView({ + el : $('#contacts'), + collection : this.conversations }); + + this.conversations.fetch({reset: true}).then(function() { + if (this.conversations.length) { + this.conversations.at(0).trigger('render'); + } + }.bind(this)); + extension.onMessage('message', function(message) { - Whisper.Conversations.fetch({id: message.conversationId}).then(function() { - Whisper.Conversations.get(message.conversationId).fetchMessages(); - }); + this.conversations.fetch({id: message.conversationId}).then(function() { + this.conversations.get(message.conversationId).fetchMessages(); + }.bind(this)); }.bind(this)); }, @@ -44,18 +49,21 @@ 'click #new-message': 'new_message', 'click #new-group': 'new_group' }, - new_message: function (e) { e.preventDefault(); $('.conversation').hide().trigger('close'); // detach any existing conversation views - this.view = new Whisper.NewConversationView(); - //todo: less new + this.view = new Whisper.NewConversationView({ + collection: this.conversations + }); + this.setContent(this.view.render().$el.show()); }, - new_group: function (e) { e.preventDefault(); $('.conversation').trigger('close'); // detach any existing conversation views - new Whisper.NewGroupView(); + var view = new Whisper.NewGroupView({ + collection: this.conversations + }); + this.setContent(view.render().$el.show()); }, resize: function (e) { var windowheight = window.innerHeight, diff --git a/js/views/new_conversation_view.js b/js/views/new_conversation_view.js index d8ea2b43..b57e26fa 100644 --- a/js/views/new_conversation_view.js +++ b/js/views/new_conversation_view.js @@ -53,7 +53,7 @@ var Whisper = Whisper || {}; initialize: function() { this.template = $('#new-message-form').html(); Mustache.parse(this.template); - this.render(); + this.$el.html($(Mustache.render(this.template))); this.input = new MessageRecipientInputView({el: this.$el.find('input.number')}); this.fileInput = new Whisper.FileInputView({el: this.$el.find('.attachments')}); }, @@ -67,7 +67,7 @@ var Whisper = Whisper || {}; e.preventDefault(); var number = this.input.verifyNumber(); if (number) { - var convo = Whisper.Conversations.findOrCreateForRecipient(number); + var convo = this.collection.findOrCreateForRecipient(number); var message_input = this.$el.find('input.send-message'); var message = message_input.val(); if (message.length > 0 || this.fileInput.hasFiles()) { @@ -79,12 +79,6 @@ var Whisper = Whisper || {}; this.remove(); convo.trigger('render'); } - }, - - render: function() { - this.$el.html(Mustache.render(this.template)); - Whisper.Layout.setContent(this.$el.show()); - return this; } }); diff --git a/js/views/new_group_view.js b/js/views/new_group_view.js index 50acd4e4..81002b41 100644 --- a/js/views/new_group_view.js +++ b/js/views/new_group_view.js @@ -23,7 +23,7 @@ var Whisper = Whisper || {}; initialize: function() { this.template = $('#new-group-form').html(); Mustache.parse(this.template); - this.render(); + this.$el.html($(Mustache.render(this.template))); this.input = this.$el.find('input.number'); new Whisper.GroupRecipientsInputView({el: this.$el.find('input.numbers')}).$el.appendTo(this.$el); }, @@ -37,17 +37,11 @@ var Whisper = Whisper || {}; var numbers = this.$el.find('input.numbers').val().split(','); var name = this.$el.find('input.name').val(); var view = this; - Whisper.Conversations.createGroup(numbers, name).then(function(convo){ + this.collection.createGroup(numbers, name).then(function(convo){ convo.sendMessage(view.$el.find('input.send-message').val()); view.remove(); convo.trigger('render'); }); - }, - - render: function() { - this.$el.prepend($(Mustache.render(this.template))); - Whisper.Layout.setContent(this.$el.show()); - return this; } }); diff --git a/test/views/message_view_test.js b/test/views/message_view_test.js index 9a1ad3d2..6f449011 100644 --- a/test/views/message_view_test.js +++ b/test/views/message_view_test.js @@ -1,9 +1,10 @@ describe('MessageView', function() { + var conversations = new Whisper.ConversationCollection(); before(function(done) { - Whisper.Conversations.fetch().then(done); + conversations.fetch().then(done); }); - var convo = Whisper.Conversations.add({id: 'foo'}); + var convo = conversations.add({id: 'foo'}); var message = convo.messages().add({ conversationId: convo.id, body: 'hello world',