Refactor for less model duplication

This commit is contained in:
lilia 2015-08-27 12:38:51 -07:00
parent c4eac76032
commit baa55c9018
9 changed files with 71 additions and 50 deletions

View file

@ -35,7 +35,7 @@
select: function(e) {
this.$el.addClass('selected');
this.$el.trigger('select', {modelId: this.model.id});
this.$el.trigger('select', {conversation: this.model});
},
render: function() {

View file

@ -113,7 +113,8 @@
'select .gutter .contact': 'openConversation'
},
openConversation: function(e, data) {
var conversation = bg.openConversation(data.modelId);
var conversation = data.conversation;
conversation.reload();
this.conversation_stack.open(conversation);
this.hideCompose();
},

View file

@ -87,27 +87,14 @@
if (this.getRecipients().length > 1) {
this.createGroup();
} else {
this.createConversation();
var id = this.getRecipients().at(0).id;
ConversationController.findOrCreatePrivateById(id).then(function(conversation) {
conversation.save('active_at', Date.now());
this.trigger('open', { conversation: conversation });
}.bind(this));
}
},
createConversation: function() {
var conversation = new Whisper.Conversation({
id: this.getRecipients().at(0).id,
type: 'private'
});
conversation.fetch().then(function() {
this.trigger('open', { modelId: conversation.id });
}.bind(this)).fail(function() {
var saved = conversation.save(); // false or indexedDBRequest
if (saved) {
saved.then(function() {
this.trigger('open', { modelId: conversation.id });
}.bind(this));
}
}.bind(this));
},
createGroup: function() {
var name = this.$('.new-group-update-form .name').val();
if (!name.trim().length) {
@ -128,9 +115,9 @@
members: members,
active_at: Date.now()
};
var group = new Whisper.Conversation(attributes);
var group = ConversationController.create(attributes);
group.save().then(function() {
this.trigger('open', {modelId: groupId});
this.trigger('open', { conversation: group });
}.bind(this));
var now = Date.now();
var message = group.messageCollection.add({

View file

@ -91,9 +91,9 @@
})
});
this.$('.contacts').append(this.typeahead_view.el);
this.initNewContact();
this.listenTo(this.typeahead, 'reset', this.filterContacts);
this.initNewContact();
},
render_attributes: function() {
@ -132,7 +132,7 @@
// Creates a view to display a new contact
this.new_contact_view = new Whisper.ConversationListItemView({
el: this.$new_contact,
model: new Whisper.Conversation({
model: ConversationController.create({
type: 'private',
newContact: true
})
@ -146,7 +146,7 @@
},
addRecipient: function(e, data) {
this.recipients.add(this.typeahead.remove(data.modelId));
this.recipients.add(this.typeahead.remove(data.conversation.id));
this.resetTypeahead();
},