Destroy all globals

Well, not *all* globals..
This commit is contained in:
lilia 2014-11-16 16:01:28 -08:00
parent c0681beca7
commit fd3a72d435
7 changed files with 30 additions and 40 deletions

View file

@ -181,6 +181,4 @@
})); }));
} }
}); });
Whisper.Conversations = new Whisper.ConversationCollection();
})(); })();

View file

@ -28,10 +28,6 @@
if (missing.length) { if (missing.length) {
console.log("Message missing attributes: " + missing); console.log("Message missing attributes: " + missing);
} }
},
conversation: function() {
return Whisper.Conversations.get(this.get('conversationId'));
} }
}); });

View file

@ -7,7 +7,6 @@ var Whisper = Whisper || {};
tagName: 'div', tagName: 'div',
id: 'contacts', id: 'contacts',
itemView: Whisper.ConversationListItemView, itemView: Whisper.ConversationListItemView,
collection: Whisper.Conversations,
events: { events: {
'click .contact': 'select', 'click .contact': 'select',

View file

@ -27,16 +27,21 @@
window.addEventListener('resize', this.resize.bind(this)); window.addEventListener('resize', this.resize.bind(this));
this.conversations = new Whisper.ConversationCollection(); this.conversations = new Whisper.ConversationCollection();
new Whisper.ConversationListView({el: $('#contacts'), collection: Whisper.Conversations}); new Whisper.ConversationListView({
Whisper.Conversations.fetch({reset: true}).then(function() { el : $('#contacts'),
if (Whisper.Conversations.length) { collection : this.conversations
Whisper.Conversations.at(0).trigger('render'); });
this.conversations.fetch({reset: true}).then(function() {
if (this.conversations.length) {
this.conversations.at(0).trigger('render');
} }
}); }.bind(this));
extension.onMessage('message', function(message) { extension.onMessage('message', function(message) {
Whisper.Conversations.fetch({id: message.conversationId}).then(function() { this.conversations.fetch({id: message.conversationId}).then(function() {
Whisper.Conversations.get(message.conversationId).fetchMessages(); this.conversations.get(message.conversationId).fetchMessages();
}); }.bind(this));
}.bind(this)); }.bind(this));
}, },
@ -44,18 +49,21 @@
'click #new-message': 'new_message', 'click #new-message': 'new_message',
'click #new-group': 'new_group' 'click #new-group': 'new_group'
}, },
new_message: function (e) { new_message: function (e) {
e.preventDefault(); e.preventDefault();
$('.conversation').hide().trigger('close'); // detach any existing conversation views $('.conversation').hide().trigger('close'); // detach any existing conversation views
this.view = new Whisper.NewConversationView(); this.view = new Whisper.NewConversationView({
//todo: less new collection: this.conversations
});
this.setContent(this.view.render().$el.show());
}, },
new_group: function (e) { new_group: function (e) {
e.preventDefault(); e.preventDefault();
$('.conversation').trigger('close'); // detach any existing conversation views $('.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) { resize: function (e) {
var windowheight = window.innerHeight, var windowheight = window.innerHeight,

View file

@ -53,7 +53,7 @@ var Whisper = Whisper || {};
initialize: function() { initialize: function() {
this.template = $('#new-message-form').html(); this.template = $('#new-message-form').html();
Mustache.parse(this.template); Mustache.parse(this.template);
this.render(); this.$el.html($(Mustache.render(this.template)));
this.input = new MessageRecipientInputView({el: this.$el.find('input.number')}); this.input = new MessageRecipientInputView({el: this.$el.find('input.number')});
this.fileInput = new Whisper.FileInputView({el: this.$el.find('.attachments')}); this.fileInput = new Whisper.FileInputView({el: this.$el.find('.attachments')});
}, },
@ -67,7 +67,7 @@ var Whisper = Whisper || {};
e.preventDefault(); e.preventDefault();
var number = this.input.verifyNumber(); var number = this.input.verifyNumber();
if (number) { 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_input = this.$el.find('input.send-message');
var message = message_input.val(); var message = message_input.val();
if (message.length > 0 || this.fileInput.hasFiles()) { if (message.length > 0 || this.fileInput.hasFiles()) {
@ -79,12 +79,6 @@ var Whisper = Whisper || {};
this.remove(); this.remove();
convo.trigger('render'); convo.trigger('render');
} }
},
render: function() {
this.$el.html(Mustache.render(this.template));
Whisper.Layout.setContent(this.$el.show());
return this;
} }
}); });

View file

@ -23,7 +23,7 @@ var Whisper = Whisper || {};
initialize: function() { initialize: function() {
this.template = $('#new-group-form').html(); this.template = $('#new-group-form').html();
Mustache.parse(this.template); Mustache.parse(this.template);
this.render(); this.$el.html($(Mustache.render(this.template)));
this.input = this.$el.find('input.number'); this.input = this.$el.find('input.number');
new Whisper.GroupRecipientsInputView({el: this.$el.find('input.numbers')}).$el.appendTo(this.$el); 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 numbers = this.$el.find('input.numbers').val().split(',');
var name = this.$el.find('input.name').val(); var name = this.$el.find('input.name').val();
var view = this; 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()); convo.sendMessage(view.$el.find('input.send-message').val());
view.remove(); view.remove();
convo.trigger('render'); convo.trigger('render');
}); });
},
render: function() {
this.$el.prepend($(Mustache.render(this.template)));
Whisper.Layout.setContent(this.$el.show());
return this;
} }
}); });

View file

@ -1,9 +1,10 @@
describe('MessageView', function() { describe('MessageView', function() {
var conversations = new Whisper.ConversationCollection();
before(function(done) { 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({ var message = convo.messages().add({
conversationId: convo.id, conversationId: convo.id,
body: 'hello world', body: 'hello world',