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

@ -282,6 +282,8 @@
<script type="text/javascript" src="js/models/conversations.js"></script>
<script type="text/javascript" src="js/chromium.js"></script>
<script type="text/javascript" src="js/bimap.js"></script>
<script type="text/javascript" src="js/panel_controller.js"></script>
<script type="text/javascript" src="js/views/whisper_view.js"></script>
<script type="text/javascript" src="js/views/toast_view.js"></script>
@ -306,8 +308,6 @@
<script type="text/javascript" src="js/views/inbox_view.js"></script>
<script type="text/javascript" src="js/views/confirmation_dialog_view.js"></script>
<script type="text/javascript" src="js/bimap.js"></script>
<script type="text/javascript" src="js/panel_controller.js"></script>
<script type="text/javascript" src="js/background.js"></script>
</head>
<body>

View file

@ -65,7 +65,7 @@
function onContactReceived(ev) {
var contactDetails = ev.contactDetails;
new Whisper.Conversation({
ConversationController.create({
name: contactDetails.name,
id: contactDetails.number,
avatar: contactDetails.avatar,
@ -75,7 +75,7 @@
function onGroupReceived(ev) {
var groupDetails = ev.groupDetails;
new Whisper.Conversation({
ConversationController.create({
id: groupDetails.id,
name: groupDetails.name,
members: groupDetails.members,

View file

@ -163,6 +163,7 @@
},
fetchMessages: function() {
if(!this.id) { return false; }
return this.messageCollection.fetchConversation(this.id);
},
@ -181,6 +182,11 @@
}
},
reload: function() {
this.fetch().then(this.fetchContacts.bind(this));
this.fetchMessages();
},
archive: function() {
this.set({active_at: null});
},

View file

@ -136,7 +136,7 @@
if (dataMessage.group) {
conversationId = dataMessage.group.id;
}
var conversation = new Whisper.Conversation({id: conversationId});
var conversation = ConversationController.create({id: conversationId});
conversation.fetch().always(function() {
var now = new Date().getTime();
var attributes = { type: 'private' };
@ -218,6 +218,7 @@
});
}
conversation.messageCollection.add(message);
conversation.save().then(function() {
message.save().then(function() {
updateInbox();

View file

@ -43,15 +43,50 @@
}
});
window.updateInbox = function() {
conversations.fetchActive().then(function() {
inbox.reset(conversations.filter(function(model) {
return model.get('active_at');
}));
});
window.ConversationController = {
create: function(attrs) {
var conversation = conversations.add(attrs);
if (conversation.get('active_at')) {
inbox.add(conversation);
}
return conversation;
},
findOrCreatePrivateById: function(id) {
var conversation = conversations.add({ id: id, type: 'private' });
return new Promise(function(resolve, reject) {
conversation.fetch().then(function() {
resolve(conversation);
}).fail(function() {
var saved = conversation.save(); // false or indexedDBRequest
if (saved) {
saved.then(function() {
resolve(conversation);
}).fail(reject);
}
reject();
});
});
},
findById: function(id) {
var conversation = conversations.add({id: id});
conversation.fetch();
return conversation;
},
updateInbox: function() {
conversations.fetchActive().then(function() {
inbox.reset(conversations.filter(function(model) {
return model.get('active_at');
}));
});
}
};
updateInbox();
window.updateInbox = function() { // TODO: remove
ConversationController.updateInbox();
};
conversations.on('change:active_at', ConversationController.updateInbox);
ConversationController.updateInbox();
setUnreadCount(storage.get("unreadCount", 0));
function setUnreadCount(count) {
@ -78,12 +113,6 @@
windowMap.remove('windowId', windowId);
}
window.getConversation = function(attrs) {
var conversation = window.inbox.get(attrs.id) || attrs;
conversation = conversations.add(conversation);
return conversation;
};
window.notifyConversation = function(message) {
var conversationId = message.get('conversationId');
if (inboxOpened) {
@ -91,8 +120,8 @@
updateConversation(conversationId);
extension.windows.drawAttention(inboxWindowId);
} else if (Whisper.Notifications.isEnabled()) {
var conversation = getConversation({id: message.get('conversationId')});
var sender = getConversation({id: message.get('source')});
var conversation = conversations.add({id: conversationId});
var sender = conversations.add({id: message.get('source')});
conversation.fetch().then(function() {
sender.fetch().then(function() {
var notification = new Notification(sender.getTitle(), {
@ -114,11 +143,8 @@
};
window.openConversation = function openConversation (modelId) {
var conversation = getConversation({id: modelId});
conversation.fetch().then(function() {
conversation.fetchContacts();
});
conversation.fetchMessages();
var conversation = conversations.add({id: modelId});
conversation.reload();
return conversation;
};

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();
},