From 9211c60495b7e122c43c1db5f9aa7ba08c476c8c Mon Sep 17 00:00:00 2001 From: Tom Coleman Date: Sun, 25 Nov 2012 19:10:10 +1100 Subject: [PATCH] Simplified subscriptions greatly (I think). --- client/app.js | 164 +++++++++++++--------------- client/views/posts/posts_digest.js | 4 +- client/views/posts/posts_new.js | 6 +- client/views/posts/posts_pending.js | 6 +- client/views/posts/posts_top.js | 6 +- 5 files changed, 86 insertions(+), 100 deletions(-) diff --git a/client/app.js b/client/app.js index 36b04d99b..491bfa12a 100644 --- a/client/app.js +++ b/client/app.js @@ -54,104 +54,90 @@ STATUS_PENDING=1; STATUS_APPROVED=2; STATUS_REJECTED=3; FIND_APPROVED={$or: [{status: {$exists : false}}, {status: STATUS_APPROVED}]}; - -// TOP page TOP_PAGE_PER_PAGE = 10; -TOP_PAGE_SORT = {score: -1}; -Session.set('topPageLimit', TOP_PAGE_PER_PAGE); -Meteor.autosubscribe(function() { - Session.set('topPostsReady', false); - Meteor.subscribe('posts', FIND_APPROVED, { - sort: TOP_PAGE_SORT, - limit: Session.get('topPageLimit') - }, function() { - Session.set('topPostsReady', true); - }); -}); -var topPosts = function() { - var orderedPosts = Posts.find(FIND_APPROVED, {sort: TOP_PAGE_SORT}); - return limitDocuments(orderedPosts, Session.get('topPageLimit')); -} - -// NEW page NEW_PAGE_PER_PAGE = 10; -NEW_PAGE_SORT = {submitted: -1}; -Session.set('newPageLimit', NEW_PAGE_PER_PAGE); -Meteor.autosubscribe(function() { - Session.set('newPostsReady', false); - // note: should use FIND_APPROVED, but this is a temporary workaround because of bug - Meteor.subscribe('posts', {userId:{$exists: true},$or: [{status: {$exists : false}}, {status: STATUS_APPROVED}]}, { - sort: NEW_PAGE_SORT, - limit: Session.get('newPageLimit') - }, function() { - Session.set('newPostsReady', true); - }); -}); -var newPosts = function() { - var orderedPosts = Posts.find(FIND_APPROVED, {sort: NEW_PAGE_SORT}); - return limitDocuments(orderedPosts, Session.get('newPageLimit')); -} - -PENDING_FIND = {$or: [{status: STATUS_PENDING}, {status: STATUS_REJECTED}]}; -// PENDING_FIND = {}; -// PENDING page -Meteor.autosubscribe(function() { - Session.set('pendingPostsReady', false); - Meteor.subscribe('posts', PENDING_FIND, { - sort: NEW_PAGE_SORT, - limit: Session.get('newPageLimit') - }, function() { - Session.set('pendingPostsReady', true); - }); -}); -var pendingPosts = function() { - var orderedPosts = Posts.find( PENDING_FIND, {sort: NEW_PAGE_SORT}); - return limitDocuments(orderedPosts, Session.get('newPageLimit')); -} - -// DIGEST page +PENDING_PAGE_PER_PAGE = 10; DIGEST_PAGE_PER_PAGE = 5; -DIGEST_PAGE_SORT = {score: -1}; -var digestPageFind = function(mDate) { - var find = { - submitted: { - $gte: mDate.startOf('day').valueOf(), - $lt: mDate.endOf('day').valueOf() + +// name <- the name of various session vars that will be set: +// - 'nameReady' <- is the subscription loading or ready? +// - 'nameLimit' <- how many of this type are we currently displaying? +// options: +// - find <- how to find the items +// - sort <- how to sort them +// - perPage <- how many to display per-page +// - +postsForSub = {}; +setupPostSubscription = function(name, options) { + var readyName = name + 'Ready'; + var limitName = name + 'Limit'; + + if (options.perPage && ! Session.get(limitName)) + Session.set(limitName, options.perPage); + + // setup the subscription + Meteor.autosubscribe(function() { + Session.set(readyName, false); + + var findOptions = { + sort: options.sort, + limit: options.perPage && Session.get(limitName) + }; + + var find = _.isFunction(options.find) ? options.find() : options.find; + Meteor.subscribe('posts', find || {}, findOptions, function() { + Session.set(readyName, true); + }); + }); + + // setup a function to find the relevant posts (+deal with mm's lack of limit) + postsForSub[name] = function() { + var find = _.isFunction(options.find) ? options.find() : options.find; + var orderedPosts = Posts.find(find || {}, {sort: options.sort}); + if (options.perPage) { + return limitDocuments(orderedPosts, Session.get(limitName)); + } else { + return orderedPosts; } }; - find=_.extend(find, FIND_APPROVED); - return find; } -Session.set('digestPageLimit', DIGEST_PAGE_PER_PAGE); -Meteor.autosubscribe(function() { - Session.set('digestPostsReady', false); - - var mDate = moment(Session.get('currentDate')); - // start yesterday, and subscribe to 3 days - mDate.add('days', -1); - for (var i = 0; i < 3; i++) { - Meteor.subscribe('posts', digestPageFind(mDate), { - sort: DIGEST_PAGE_SORT, - limit: Session.get('digestPageLimit') - }, function() { - Session.set('digestPostsReady', true); - }); - mDate.add('days', 1); - } +setupPostSubscription('singlePost', { + find: function() { return Session.get('selectedPostId'); } }); -var digestPosts = function() { - var mDate = moment(Session.get('currentDate')) - var orderedPosts = Posts.find(digestPageFind(mDate), {sort: DIGEST_PAGE_SORT}); - return limitDocuments(orderedPosts, Session.get('digestPageLimit')); -} -// SINGLE post, e.g. post_page -Meteor.autosubscribe(function() { - Session.set('postReady', false); - Meteor.subscribe('post', Session.get('selectedPostId'), function() { - Session.set('postReady', true); - }) +setupPostSubscription('topPosts', { + find: FIND_APPROVED, + sort: {score: -1}, + perPage: TOP_PAGE_PER_PAGE +}); + +setupPostSubscription('newPosts', { + find: FIND_APPROVED, + sort: {submitted: -1}, + perPage: NEW_PAGE_PER_PAGE +}); + +setupPostSubscription('pendingPosts', { + find: {$or: [{status: STATUS_PENDING}, {status: STATUS_REJECTED}]}, + sort: {score: -1}, + perPage: PENDING_PAGE_PER_PAGE +}); + +setupPostSubscription('digestPosts', { + find: function() { + var mDate = moment(Session.get('currentDate')); + var find = { + submitted: { + $gte: mDate.startOf('day').valueOf(), + $lt: mDate.endOf('day').valueOf() + } + }; + find=_.extend(find, FIND_APPROVED); + return find; + }, + sort: {score: -1}, + perPage: DIGEST_PAGE_PER_PAGE }); // ** Categories ** diff --git a/client/views/posts/posts_digest.js b/client/views/posts/posts_digest.js index a37aec796..6713d7460 100644 --- a/client/views/posts/posts_digest.js +++ b/client/views/posts/posts_digest.js @@ -1,9 +1,9 @@ Template.posts_digest.helpers({ posts: function(){ - return digestPosts(); + return postsForSub.digestPosts(); }, hasPosts: function(){ - return !!digestPosts().length; + return !!postsForSub.digestPosts().length; }, currentDate: function(){ return moment(Session.get('currentDate')).format("dddd, MMMM Do YYYY"); diff --git a/client/views/posts/posts_new.js b/client/views/posts/posts_new.js index eae7ef291..777d5ff79 100644 --- a/client/views/posts/posts_new.js +++ b/client/views/posts/posts_new.js @@ -1,10 +1,10 @@ Template.posts_new.posts = function() { - return newPosts(); + return postsForSub.newPosts(); }; Template.posts_new.helpers({ allPostsLoaded: function(){ - return newPosts().length < Session.get('newPageLimit'); + return postsForSub.newPosts().length < Session.get('newPostsLimit'); } }); @@ -21,6 +21,6 @@ Template.posts_new.events({ 'click .more-link': function(e) { e.preventDefault(); Session.set('currentScroll',$('body').scrollTop()); - Session.set('newPageLimit', Session.get('newPageLimit') + NEW_PAGE_PER_PAGE) + Session.set('newPostsLimit', Session.get('newPostsLimit') + NEW_PAGE_PER_PAGE) } }); diff --git a/client/views/posts/posts_pending.js b/client/views/posts/posts_pending.js index 2a96eab70..ccbf4d213 100644 --- a/client/views/posts/posts_pending.js +++ b/client/views/posts/posts_pending.js @@ -1,10 +1,10 @@ Template.posts_pending.posts = function() { - return pendingPosts(); + return postsForSub.pendingPosts(); }; Template.posts_pending.helpers({ allPostsLoaded: function(){ - return pendingPosts().length < Session.get('newPageLimit'); + return postsForSub.pendingPosts().length < Session.get('pendingPostsLimit'); } }); @@ -20,6 +20,6 @@ Template.posts_pending.events({ 'click .more-link': function(e) { e.preventDefault(); Session.set('currentScroll',$('body').scrollTop()); - Session.set('newPageLimit', Session.get('newPageLimit') + NEW_PAGE_PER_PAGE) + Session.set('pendingPostsLimit', Session.get('pendingPostsLimit') + NEW_PAGE_PER_PAGE) } }); diff --git a/client/views/posts/posts_top.js b/client/views/posts/posts_top.js index 20398672e..d97f1d12d 100644 --- a/client/views/posts/posts_top.js +++ b/client/views/posts/posts_top.js @@ -1,10 +1,10 @@ Template.posts_top.posts = function() { - return topPosts(); + return postsForSub.topPosts(); }; Template.posts_top.helpers({ allPostsLoaded: function(){ - return topPosts().length < Session.get('topPageLimit'); + return postsForSub.topPosts().length < Session.get('topPostsLimit'); } }); @@ -21,7 +21,7 @@ Template.posts_top.events({ 'click .more-link': function(e) { e.preventDefault(); Session.set('currentScroll',$('body').scrollTop()); - Session.set('topPageLimit', Session.get('topPageLimit') + TOP_PAGE_PER_PAGE); + Session.set('topPostsLimit', Session.get('topPostsLimit') + TOP_PAGE_PER_PAGE); } });