Use view.$ shorthand for scoped jquery searches

Wish I'd noticed that one earlier. http://backbonejs.org/#View-dollar
This commit is contained in:
lilia 2015-03-27 18:47:58 -07:00
parent 0373252901
commit 5d4298697c
10 changed files with 48 additions and 46 deletions

View file

@ -22,15 +22,17 @@
extension.windows.getCurrent(function (windowInfo) { extension.windows.getCurrent(function (windowInfo) {
var bg = extension.windows.getBackground(); var bg = extension.windows.getBackground();
window.$ = bg.$; window.$ = bg.$;
var body = $('body', document)
var conversation = bg.getConversationForWindow(windowInfo.id); var conversation = bg.getConversationForWindow(windowInfo.id);
if (conversation) { if (conversation) {
window.document.title = conversation.getTitle(); window.document.title = conversation.getTitle();
new bg.Whisper.ConversationView({ var view = new bg.Whisper.ConversationView({
model: conversation model: conversation
}).$el.prependTo($('body', document)); });
$('input.send-message', document).focus(); view.$el.prependTo(body);
view.$('input.send-message').focus();
} else { } else {
$('<div>').text('Error').prependTo($('body', document)); $('<div>').text('Error').prependTo(body);
} }
}); });
}()); }());

View file

@ -32,13 +32,13 @@
this.render(); this.render();
this.fileInput = new Whisper.FileInputView({ this.fileInput = new Whisper.FileInputView({
el: this.$el.find('.attachments') el: this.$('.attachments')
}); });
this.view = new Whisper.MessageListView({ this.view = new Whisper.MessageListView({
collection: this.model.messageCollection collection: this.model.messageCollection
}); });
this.$el.find('.discussion-container').append(this.view.el); this.$('.discussion-container').append(this.view.el);
this.view.render(); this.view.render();
setTimeout(function() { setTimeout(function() {
@ -106,22 +106,22 @@
closeMenu: function(e) { closeMenu: function(e) {
if (e && !$(e.target).hasClass('hamburger')) { if (e && !$(e.target).hasClass('hamburger')) {
this.$el.find('.menu-list').hide(); this.$('.menu-list').hide();
} }
}, },
endSession: function() { endSession: function() {
this.model.endSession(); this.model.endSession();
this.$el.find('.menu-list').hide(); this.$('.menu-list').hide();
}, },
leaveGroup: function() { leaveGroup: function() {
this.model.leaveGroup(); this.model.leaveGroup();
this.$el.find('.menu-list').hide(); this.$('.menu-list').hide();
}, },
toggleMenu: function() { toggleMenu: function() {
this.$el.find('.menu-list').toggle(); this.$('.menu-list').toggle();
}, },
newGroupUpdate: function() { newGroupUpdate: function() {
@ -137,12 +137,12 @@
if (confirm("Permanently delete this conversation?")) { if (confirm("Permanently delete this conversation?")) {
this.model.destroyMessages(); this.model.destroyMessages();
} }
this.$el.find('.menu-list').hide(); this.$('.menu-list').hide();
}, },
sendMessage: function(e) { sendMessage: function(e) {
e.preventDefault(); e.preventDefault();
var input = this.$el.find('.send input.send-message'); var input = this.$('.send input.send-message');
var message = this.replace_colons(input.val()); var message = this.replace_colons(input.val());
var convo = this.model; var convo = this.model;

View file

@ -25,10 +25,10 @@
tagName: 'span', tagName: 'span',
className: 'file-input', className: 'file-input',
initialize: function() { initialize: function() {
this.$input = this.$el.find('input[type=file]'); this.$input = this.$('input[type=file]');
this.thumb = new Whisper.AttachmentPreviewView(); this.thumb = new Whisper.AttachmentPreviewView();
this.$el.addClass('file-input'); this.$el.addClass('file-input');
this.$default = this.$el.find('.default'); this.$default = this.$('.default');
}, },
events: { events: {
@ -44,7 +44,7 @@
addThumb: function(src) { addThumb: function(src) {
this.$default.hide(); this.$default.hide();
this.thumb.src = src; this.thumb.src = src;
this.$el.find('.thumbnail').append(this.thumb.render().el); this.$('.thumbnail').append(this.thumb.render().el);
}, },
autoScale: function(file) { autoScale: function(file) {

View file

@ -65,13 +65,13 @@
this.openConversation.bind(this, null)); this.openConversation.bind(this, null));
this.inbox = new Whisper.ConversationListView({ this.inbox = new Whisper.ConversationListView({
el : this.$el.find('.conversations'), el : this.$('.conversations'),
collection : bg.inbox collection : bg.inbox
}).render(); }).render();
this.inbox.listenTo(bg.inbox, 'sort', this.inbox.render); this.inbox.listenTo(bg.inbox, 'sort', this.inbox.render);
new SocketView().render().$el.appendTo(this.$el.find('.socket-status')); new SocketView().render().$el.appendTo(this.$('.socket-status'));
window.addEventListener('beforeunload', function () { window.addEventListener('beforeunload', function () {
this.inbox.stopListening(); this.inbox.stopListening();

View file

@ -95,14 +95,14 @@
received_at : moment(this.model.get('received_at')).toString(), received_at : moment(this.model.get('received_at')).toString(),
tofrom : this.model.isIncoming() ? 'From' : 'To', tofrom : this.model.isIncoming() ? 'From' : 'To',
})); }));
this.view.render().$el.prependTo(this.$el.find('.message-container')); this.view.render().$el.prependTo(this.$('.message-container'));
if (this.model.isOutgoing()) { if (this.model.isOutgoing()) {
this.conversation.contactCollection.each(function(contact) { this.conversation.contactCollection.each(function(contact) {
var v = new ContactView({ var v = new ContactView({
model: contact, model: contact,
conflict: this.model.getKeyConflict(contact.id) conflict: this.model.getKeyConflict(contact.id)
}).render().$el.appendTo(this.$el.find('.contacts')); }).render().$el.appendTo(this.$('.contacts'));
}.bind(this)); }.bind(this));
} else { } else {
var number = this.model.get('source'); var number = this.model.get('source');
@ -110,7 +110,7 @@
var v = new ContactView({ var v = new ContactView({
model: contact, model: contact,
conflict: this.model.getKeyConflict(number) conflict: this.model.getKeyConflict(number)
}).render().$el.appendTo(this.$el.find('.contacts')); }).render().$el.appendTo(this.$('.contacts'));
} }
} }
}); });

View file

@ -41,7 +41,7 @@
renderControl: function() { renderControl: function() {
if (this.model.isEndSession() || this.model.isGroupUpdate()) { if (this.model.isEndSession() || this.model.isGroupUpdate()) {
this.$el.addClass('control'); this.$el.addClass('control');
this.$el.find('.content').text(this.model.getDescription()); this.$('.content').text(this.model.getDescription());
} else { } else {
this.$el.removeClass('control'); this.$el.removeClass('control');
} }
@ -62,13 +62,13 @@
twemoji.parse(this.el, { base: '/images/twemoji/', size: 16 }); twemoji.parse(this.el, { base: '/images/twemoji/', size: 16 });
var content = this.$el.find('.content'); var content = this.$('.content');
content.html(this.autoLink(content.html())); content.html(this.autoLink(content.html()));
this.renderDelivered(); this.renderDelivered();
this.renderControl(); this.renderControl();
this.$el.find('.attachments').append( this.$('.attachments').append(
this.model.get('attachments').map(function(attachment) { this.model.get('attachments').map(function(attachment) {
return new Whisper.AttachmentView({ return new Whisper.AttachmentView({
model: attachment model: attachment
@ -78,7 +78,7 @@
var errors = this.model.get('errors'); var errors = this.model.get('errors');
if (errors && errors.length) { if (errors && errors.length) {
this.$el.find('.bubble').prepend( this.$('.bubble').prepend(
errors.map(function(error) { errors.map(function(error) {
return new Whisper.MessageErrorView({ return new Whisper.MessageErrorView({
model: error, model: error,

View file

@ -22,17 +22,17 @@
template: $('#new-conversation').html(), template: $('#new-conversation').html(),
initialize: function() { initialize: function() {
this.render(); this.render();
this.$group_update = this.$el.find('.new-group-update-form'); this.$group_update = this.$('.new-group-update-form');
this.$create = this.$el.find('.create'); this.$create = this.$('.create');
this.$input = this.$el.find('input.search'); this.$input = this.$('input.search');
// Group avatar file input // Group avatar file input
this.avatarInput = new Whisper.FileInputView({ this.avatarInput = new Whisper.FileInputView({
el: this.$el.find('.group-avatar') el: this.$('.group-avatar')
}); });
this.recipients_view = new Whisper.RecipientsInputView(); this.recipients_view = new Whisper.RecipientsInputView();
this.$el.find('.scrollable').append(this.recipients_view.el); this.$('.scrollable').append(this.recipients_view.el);
this.listenTo(this.getRecipients(), 'add', this.updateControls); this.listenTo(this.getRecipients(), 'add', this.updateControls);
this.listenTo(this.getRecipients(), 'remove', this.updateControls); this.listenTo(this.getRecipients(), 'remove', this.updateControls);
}, },
@ -72,7 +72,7 @@
}, },
create: function() { create: function() {
var errors = this.recipients_view.$el.find('.error'); var errors = this.recipients_view.$('.error');
if (errors.length) { if (errors.length) {
// TODO: css animation or error notification // TODO: css animation or error notification
@ -107,7 +107,7 @@
}, },
createGroup: function() { createGroup: function() {
var name = this.$el.find('.new-group-update-form .name').val(); var name = this.$('.new-group-update-form .name').val();
if (!name.trim().length) { if (!name.trim().length) {
return; return;
} }
@ -153,7 +153,7 @@
reset: function() { reset: function() {
this.$create.hide(); this.$create.hide();
this.$el.find('.new-group-update-form .name').val(''); this.$('.new-group-update-form .name').val('');
this.$group_update.hide(); this.$group_update.hide();
this.recipients_view.reset(); this.recipients_view.reset();
}, },

View file

@ -24,12 +24,12 @@
initialize: function(options) { initialize: function(options) {
this.render(); this.render();
this.avatarInput = new Whisper.FileInputView({ this.avatarInput = new Whisper.FileInputView({
el: this.$el.find('.group-avatar') el: this.$('.group-avatar')
}); });
this.recipients_view = new Whisper.RecipientsInputView(); this.recipients_view = new Whisper.RecipientsInputView();
this.$el.find('.scrollable').append(this.recipients_view.el); this.$('.scrollable').append(this.recipients_view.el);
this.$el.find('.avatar').addClass('default'); this.$('.avatar').addClass('default');
}, },
events: { events: {
'click .back': 'goBack', 'click .back': 'goBack',
@ -47,7 +47,7 @@
send: function() { send: function() {
return this.avatarInput.getFiles().then(function(avatarFiles) { return this.avatarInput.getFiles().then(function(avatarFiles) {
this.model.save({ this.model.save({
name: this.$el.find('.name').val(), name: this.$('.name').val(),
avatar: avatarFiles[0], avatar: avatarFiles[0],
members: _.union(this.model.get('members'), this.recipients_view.recipients.pluck('id')) members: _.union(this.model.get('members'), this.recipients_view.recipients.pluck('id'))
}); });

View file

@ -23,7 +23,7 @@
template: $('#phone-number').html(), template: $('#phone-number').html(),
render: function() { render: function() {
this.$el.html($(Mustache.render(this.template))); this.$el.html($(Mustache.render(this.template)));
this.$el.find('input.number').intlTelInput(); this.$('input.number').intlTelInput();
return this; return this;
}, },
@ -33,18 +33,18 @@
}, },
validateNumber: function() { validateNumber: function() {
var input = this.$el.find('input.number'); var input = this.$('input.number');
try { try {
var regionCode = this.$el.find('li.active').attr('data-country-code').toUpperCase(); var regionCode = this.$('li.active').attr('data-country-code').toUpperCase();
var number = input.val(); var number = input.val();
var parsedNumber = libphonenumber.util.verifyNumber(number, regionCode); var parsedNumber = libphonenumber.util.verifyNumber(number, regionCode);
this.$el.find('.number-container').removeClass('invalid'); this.$('.number-container').removeClass('invalid');
this.$el.find('.number-container').addClass('valid'); this.$('.number-container').addClass('valid');
return parsedNumber; return parsedNumber;
} catch(e) { } catch(e) {
this.$el.find('.number-container').removeClass('valid'); this.$('.number-container').removeClass('valid');
} finally { } finally {
input.trigger('validation'); input.trigger('validation');
} }

View file

@ -60,8 +60,8 @@
template: $('#recipients-input').html(), template: $('#recipients-input').html(),
initialize: function() { initialize: function() {
this.render(); this.render();
this.$input = this.$el.find('input.search'); this.$input = this.$('input.search');
this.$new_contact = this.$el.find('.new-contact'); this.$new_contact = this.$('.new-contact');
// Collection of recipients selected for the new message // Collection of recipients selected for the new message
this.recipients = new Whisper.ConversationCollection([], { this.recipients = new Whisper.ConversationCollection([], {
@ -71,7 +71,7 @@
// View to display the selected recipients // View to display the selected recipients
this.recipients_view = new Whisper.RecipientListView({ this.recipients_view = new Whisper.RecipientListView({
collection: this.recipients, collection: this.recipients,
el: this.$el.find('.recipients') el: this.$('.recipients')
}); });
// Collection of contacts to match user input against // Collection of contacts to match user input against
@ -84,7 +84,7 @@
comparator: function(m) { return m.getTitle(); } comparator: function(m) { return m.getTitle(); }
}) })
}); });
this.$el.find('.contacts').append(this.typeahead_view.el); this.$('.contacts').append(this.typeahead_view.el);
this.initNewContact(); this.initNewContact();
}, },