From 2850d563fda802bd816dcbfc81ca1fd981214d98 Mon Sep 17 00:00:00 2001 From: Sacha Greif Date: Mon, 8 Dec 2014 18:03:30 +0900 Subject: [PATCH] more refactoring --- .../users/profile/user_downvoted_posts.html | 4 +- .../users/profile/user_downvoted_posts.js | 49 +++++++++++-------- client/views/users/profile/user_posts.js | 24 +++++---- .../users/profile/user_upvoted_posts.html | 4 +- .../views/users/profile/user_upvoted_posts.js | 49 +++++++++++-------- 5 files changed, 77 insertions(+), 53 deletions(-) diff --git a/client/views/users/profile/user_downvoted_posts.html b/client/views/users/profile/user_downvoted_posts.html index 821150c68..fd0cb3f51 100644 --- a/client/views/users/profile/user_downvoted_posts.html +++ b/client/views/users/profile/user_downvoted_posts.html @@ -8,13 +8,13 @@ Downvoted At - {{#each downvoted}} + {{#each posts}} {{title}} {{formatDate votedAt "MM/DD/YYYY, HH:mm"}} {{/each}} - {{#if hasMoreDownvotedPosts}} + {{#if hasMorePosts}} {{_ "load_more"}} diff --git a/client/views/users/profile/user_downvoted_posts.js b/client/views/users/profile/user_downvoted_posts.js index cd31cd438..39a0c9a06 100644 --- a/client/views/users/profile/user_downvoted_posts.js +++ b/client/views/users/profile/user_downvoted_posts.js @@ -1,37 +1,46 @@ Template[getTemplate('userDownvotedPosts')].created = function () { - Session.set('downvotedPostsShown', 5); + var user = this.data; - var terms = {}; + var instance = this; + + // initialize the terms and posts local reactive variables + instance.terms = new ReactiveVar({ + view: 'userDownvotedPosts', + userId: user._id, + limit: 5 + }); + instance.posts = new ReactiveVar({}); + + // will re-run when the "terms" local reactive variable changes Tracker.autorun(function () { - terms = { - view: 'downvotedPosts', - userId: user._id, - limit: Session.get('downvotedPostsShown') - } + + // get the new terms and generate new parameters from them + var terms = instance.terms.get(); + var parameters = getPostsParameters(terms); + + // subscribe to the userPosts publication coreSubscriptions.subscribe('userDownvotedPosts', terms); + + // update the instance's "posts" cursor + instance.posts.set(Posts.find(parameters.find, parameters.options)); + }); }; Template[getTemplate('userDownvotedPosts')].helpers({ - downvotedPosts: function () { - // extend upvotes with each upvoted post - if(!!this.votes.downvotedPosts){ - var extendedVotes = this.votes.downvotedPosts.map(function (item) { - var post = Posts.findOne(item.itemId); - return _.extend(item, post); - }); - return _.first(extendedVotes, Session.get('downvotedPostsShown')); - } + posts: function () { + return Template.instance().posts.get(); }, - hasMoreDownvotedPosts: function () { - return !!this.votes.downvotedPosts && this.votes.downvotedPosts.length >= Session.get('downvotedPostsShown'); + hasMorePosts: function () { + return Template.instance().posts.get().count() >= Session.get('postsShown'); } }); Template[getTemplate('userDownvotedPosts')].events({ 'click .downvotedposts-more': function (e) { e.preventDefault(); - var downvotedPostsShown = Session.get('downvotedPostsShown'); - Session.set('downvotedPostsShown', downvotedPostsShown + 10); + var terms = Template.instance().terms.get(); + terms.limit += 5; + Template.instance().terms.set(terms) } }); \ No newline at end of file diff --git a/client/views/users/profile/user_posts.js b/client/views/users/profile/user_posts.js index e13192339..43e751498 100644 --- a/client/views/users/profile/user_posts.js +++ b/client/views/users/profile/user_posts.js @@ -3,30 +3,36 @@ Template[getTemplate('userPosts')].created = function () { var user = this.data; var instance = this; - // initialize the terms local reactive variable + // initialize the terms and posts local reactive variables instance.terms = new ReactiveVar({ view: 'userPosts', userId: user._id, limit: 5 }); + instance.posts = new ReactiveVar({}); // will re-run when the "terms" local reactive variable changes Tracker.autorun(function () { - coreSubscriptions.subscribe('userPosts', instance.terms.get()); + + // get the new terms and generate new parameters from them + var terms = instance.terms.get(); + var parameters = getPostsParameters(terms); + + // subscribe to the userPosts publication + coreSubscriptions.subscribe('userPosts', terms); + + // update the instance's "posts" cursor + instance.posts.set(Posts.find(parameters.find, parameters.options)); + }); }; Template[getTemplate('userPosts')].helpers({ posts: function () { - // access the reactive var on the local instance - var parameters = getPostsParameters(Template.instance().terms.get()); - var posts = Posts.find(parameters.find, parameters.options) - return posts; + return Template.instance().posts.get(); }, hasMorePosts: function () { - var parameters = getPostsParameters(Template.instance().terms.get()); - var posts = Posts.find(parameters.find, parameters.options) - return posts.count() >= Session.get('postsShown'); + return Template.instance().posts.get().count() >= Session.get('postsShown'); } }); diff --git a/client/views/users/profile/user_upvoted_posts.html b/client/views/users/profile/user_upvoted_posts.html index 6b15ca816..5cd3e4dc6 100644 --- a/client/views/users/profile/user_upvoted_posts.html +++ b/client/views/users/profile/user_upvoted_posts.html @@ -8,13 +8,13 @@ Upvoted At - {{#each upvotedPosts}} + {{#each posts}} {{title}} {{formatDate votedAt "MM/DD/YYYY, HH:mm"}} {{/each}} - {{#if hasMoreUpvotedPosts}} + {{#if hasMorePosts}} {{_ "load_more"}} diff --git a/client/views/users/profile/user_upvoted_posts.js b/client/views/users/profile/user_upvoted_posts.js index f7d381a11..8a1de7e87 100644 --- a/client/views/users/profile/user_upvoted_posts.js +++ b/client/views/users/profile/user_upvoted_posts.js @@ -1,37 +1,46 @@ Template[getTemplate('userUpvotedPosts')].created = function () { - Session.set('upvotedPostsShown', 5); + var user = this.data; - var terms = {}; + var instance = this; + + // initialize the terms and posts local reactive variables + instance.terms = new ReactiveVar({ + view: 'userUpvotedPosts', + userId: user._id, + limit: 5 + }); + instance.posts = new ReactiveVar({}); + + // will re-run when the "terms" local reactive variable changes Tracker.autorun(function () { - terms = { - view: 'upvotedPosts', - userId: user._id, - limit: Session.get('upvotedPostsShown') - } + + // get the new terms and generate new parameters from them + var terms = instance.terms.get(); + var parameters = getPostsParameters(terms); + + // subscribe to the userPosts publication coreSubscriptions.subscribe('userUpvotedPosts', terms); + + // update the instance's "posts" cursor + instance.posts.set(Posts.find(parameters.find, parameters.options)); + }); }; Template[getTemplate('userUpvotedPosts')].helpers({ - upvotedPosts: function () { - // extend upvotes with each upvoted post - if(!!this.votes.upvotedPosts){ - var extendedVotes = this.votes.upvotedPosts.map(function (item) { - var post = Posts.findOne(item.itemId); - return _.extend(item, post); - }); - return _.first(extendedVotes, Session.get('upvotedPostsShown')); - } + posts: function () { + return Template.instance().posts.get(); }, - hasMoreUpvotedPosts: function () { - return !!this.votes.upvotedPosts && this.votes.upvotedPosts.length >= Session.get('upvotedPostsShown'); + hasMorePosts: function () { + return Template.instance().posts.get().count() >= Session.get('postsShown'); } }); Template[getTemplate('userUpvotedPosts')].events({ 'click .upvotedposts-more': function (e) { e.preventDefault(); - var upvotedPostsShown = Session.get('upvotedPostsShown'); - Session.set('upvotedPostsShown', upvotedPostsShown + 10); + var terms = Template.instance().terms.get(); + terms.limit += 5; + Template.instance().terms.set(terms) } }); \ No newline at end of file