more refactoring

This commit is contained in:
Sacha Greif 2014-12-08 18:03:30 +09:00
parent e099ba3fcd
commit 2850d563fd
5 changed files with 77 additions and 53 deletions

View file

@ -8,13 +8,13 @@
<td>Downvoted At</td> <td>Downvoted At</td>
</tr> </tr>
</thead> </thead>
{{#each downvoted}} {{#each posts}}
<tr> <tr>
<td><a href="{{pathFor route='post_page' _id=_id}}">{{title}}</a></td> <td><a href="{{pathFor route='post_page' _id=_id}}">{{title}}</a></td>
<td>{{formatDate votedAt "MM/DD/YYYY, HH:mm"}}</td> <td>{{formatDate votedAt "MM/DD/YYYY, HH:mm"}}</td>
</tr> </tr>
{{/each}} {{/each}}
{{#if hasMoreDownvotedPosts}} {{#if hasMorePosts}}
<tr> <tr>
<td colspan="2"> <td colspan="2">
<a class="downvoted-more more-button grid-module" href="#"><span>{{_ "load_more"}}</span></a> <a class="downvoted-more more-button grid-module" href="#"><span>{{_ "load_more"}}</span></a>

View file

@ -1,37 +1,46 @@
Template[getTemplate('userDownvotedPosts')].created = function () { Template[getTemplate('userDownvotedPosts')].created = function () {
Session.set('downvotedPostsShown', 5);
var user = this.data; 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 () { Tracker.autorun(function () {
terms = {
view: 'downvotedPosts', // get the new terms and generate new parameters from them
userId: user._id, var terms = instance.terms.get();
limit: Session.get('downvotedPostsShown') var parameters = getPostsParameters(terms);
}
// subscribe to the userPosts publication
coreSubscriptions.subscribe('userDownvotedPosts', terms); coreSubscriptions.subscribe('userDownvotedPosts', terms);
// update the instance's "posts" cursor
instance.posts.set(Posts.find(parameters.find, parameters.options));
}); });
}; };
Template[getTemplate('userDownvotedPosts')].helpers({ Template[getTemplate('userDownvotedPosts')].helpers({
downvotedPosts: function () { posts: function () {
// extend upvotes with each upvoted post return Template.instance().posts.get();
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'));
}
}, },
hasMoreDownvotedPosts: function () { hasMorePosts: function () {
return !!this.votes.downvotedPosts && this.votes.downvotedPosts.length >= Session.get('downvotedPostsShown'); return Template.instance().posts.get().count() >= Session.get('postsShown');
} }
}); });
Template[getTemplate('userDownvotedPosts')].events({ Template[getTemplate('userDownvotedPosts')].events({
'click .downvotedposts-more': function (e) { 'click .downvotedposts-more': function (e) {
e.preventDefault(); e.preventDefault();
var downvotedPostsShown = Session.get('downvotedPostsShown'); var terms = Template.instance().terms.get();
Session.set('downvotedPostsShown', downvotedPostsShown + 10); terms.limit += 5;
Template.instance().terms.set(terms)
} }
}); });

View file

@ -3,30 +3,36 @@ Template[getTemplate('userPosts')].created = function () {
var user = this.data; var user = this.data;
var instance = this; var instance = this;
// initialize the terms local reactive variable // initialize the terms and posts local reactive variables
instance.terms = new ReactiveVar({ instance.terms = new ReactiveVar({
view: 'userPosts', view: 'userPosts',
userId: user._id, userId: user._id,
limit: 5 limit: 5
}); });
instance.posts = new ReactiveVar({});
// will re-run when the "terms" local reactive variable changes // will re-run when the "terms" local reactive variable changes
Tracker.autorun(function () { 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({ Template[getTemplate('userPosts')].helpers({
posts: function () { posts: function () {
// access the reactive var on the local instance return Template.instance().posts.get();
var parameters = getPostsParameters(Template.instance().terms.get());
var posts = Posts.find(parameters.find, parameters.options)
return posts;
}, },
hasMorePosts: function () { hasMorePosts: function () {
var parameters = getPostsParameters(Template.instance().terms.get()); return Template.instance().posts.get().count() >= Session.get('postsShown');
var posts = Posts.find(parameters.find, parameters.options)
return posts.count() >= Session.get('postsShown');
} }
}); });

View file

@ -8,13 +8,13 @@
<td>Upvoted At</td> <td>Upvoted At</td>
</tr> </tr>
</thead> </thead>
{{#each upvotedPosts}} {{#each posts}}
<tr> <tr>
<td><a href="{{pathFor route='post_page' _id=_id}}">{{title}}</a></td> <td><a href="{{pathFor route='post_page' _id=_id}}">{{title}}</a></td>
<td>{{formatDate votedAt "MM/DD/YYYY, HH:mm"}}</td> <td>{{formatDate votedAt "MM/DD/YYYY, HH:mm"}}</td>
</tr> </tr>
{{/each}} {{/each}}
{{#if hasMoreUpvotedPosts}} {{#if hasMorePosts}}
<tr> <tr>
<td colspan="2"> <td colspan="2">
<a class="upvotedposts-more more-button grid-module" href="#"><span>{{_ "load_more"}}</span></a> <a class="upvotedposts-more more-button grid-module" href="#"><span>{{_ "load_more"}}</span></a>

View file

@ -1,37 +1,46 @@
Template[getTemplate('userUpvotedPosts')].created = function () { Template[getTemplate('userUpvotedPosts')].created = function () {
Session.set('upvotedPostsShown', 5);
var user = this.data; 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 () { Tracker.autorun(function () {
terms = {
view: 'upvotedPosts', // get the new terms and generate new parameters from them
userId: user._id, var terms = instance.terms.get();
limit: Session.get('upvotedPostsShown') var parameters = getPostsParameters(terms);
}
// subscribe to the userPosts publication
coreSubscriptions.subscribe('userUpvotedPosts', terms); coreSubscriptions.subscribe('userUpvotedPosts', terms);
// update the instance's "posts" cursor
instance.posts.set(Posts.find(parameters.find, parameters.options));
}); });
}; };
Template[getTemplate('userUpvotedPosts')].helpers({ Template[getTemplate('userUpvotedPosts')].helpers({
upvotedPosts: function () { posts: function () {
// extend upvotes with each upvoted post return Template.instance().posts.get();
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'));
}
}, },
hasMoreUpvotedPosts: function () { hasMorePosts: function () {
return !!this.votes.upvotedPosts && this.votes.upvotedPosts.length >= Session.get('upvotedPostsShown'); return Template.instance().posts.get().count() >= Session.get('postsShown');
} }
}); });
Template[getTemplate('userUpvotedPosts')].events({ Template[getTemplate('userUpvotedPosts')].events({
'click .upvotedposts-more': function (e) { 'click .upvotedposts-more': function (e) {
e.preventDefault(); e.preventDefault();
var upvotedPostsShown = Session.get('upvotedPostsShown'); var terms = Template.instance().terms.get();
Session.set('upvotedPostsShown', upvotedPostsShown + 10); terms.limit += 5;
Template.instance().terms.set(terms)
} }
}); });