From cfd4ccc8037e0c02527516e896f5196725d7f76e Mon Sep 17 00:00:00 2001 From: lilia Date: Fri, 19 Dec 2014 12:55:29 -0800 Subject: [PATCH] Move index querying logic to /models --- js/models/conversations.js | 24 ++++++++++++------------ js/models/messages.js | 16 ++++++++++++++++ js/views/inbox_view.js | 10 +--------- 3 files changed, 29 insertions(+), 21 deletions(-) diff --git a/js/models/conversations.js b/js/models/conversations.js index 707aa1b80f7..406f3d451f7 100644 --- a/js/models/conversations.js +++ b/js/models/conversations.js @@ -87,18 +87,7 @@ }, fetchMessages: function(options) { - options = options || {}; - options.index = { - // 'conversation' index on conversationId - // WHERE conversationId = this.id ORDER received_at DESC - name : 'conversation', - lower : [this.id], - upper : [this.id, Number.MAX_VALUE], - order : 'desc' - }; - return this.messageCollection.fetch(options); - // TODO pagination/infinite scroll - // limit: 10, offset: page*10, + return this.messageCollection.fetchConversation(this.id, options); }, archive: function() { @@ -187,6 +176,17 @@ only: number } }); + }, + + fetchActive: function(options) { + return this.fetch(_.extend(options, { + index: { + name: 'inbox', // 'inbox' index on active_at + order: 'desc' // ORDER timestamp DESC + } + // TODO pagination/infinite scroll + // limit: 10, offset: page*10, + })); } }); })(); diff --git a/js/models/messages.js b/js/models/messages.js index 25cf06757cf..aaea2d8edf3 100644 --- a/js/models/messages.js +++ b/js/models/messages.js @@ -57,6 +57,22 @@ only: timestamp } }); + }, + + fetchConversation: function(conversationId, options) { + options = options || {}; + options.index = { + // 'conversation' index on [conversationId, received_at] + name : 'conversation', + lower : [conversationId], + upper : [conversationId, Number.MAX_VALUE], + order : 'desc' + // SELECT messages WHERE conversationId = this.id ORDER + // received_at DESC + }; + // TODO pagination/infinite scroll + // limit: 10, offset: page*10, + return this.fetch(options); } }); })() diff --git a/js/views/inbox_view.js b/js/views/inbox_view.js index 6eb9a08c707..2644daf0abd 100644 --- a/js/views/inbox_view.js +++ b/js/views/inbox_view.js @@ -32,15 +32,7 @@ collection : this.conversations }); - this.conversations.fetch({ - index: { - name: 'inbox', // 'inbox' index on active_at - order: 'desc' // ORDER timestamp DESC - }, - reset: true - // TODO pagination/infinite scroll - // limit: 10, offset: page*10, - }).then(function() { + this.conversations.fetchActive({reset: true}).then(function() { if (this.conversations.length) { this.conversations.at(0).trigger('render'); }