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>
</tr>
</thead>
{{#each downvoted}}
{{#each posts}}
<tr>
<td><a href="{{pathFor route='post_page' _id=_id}}">{{title}}</a></td>
<td>{{formatDate votedAt "MM/DD/YYYY, HH:mm"}}</td>
</tr>
{{/each}}
{{#if hasMoreDownvotedPosts}}
{{#if hasMorePosts}}
<tr>
<td colspan="2">
<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 () {
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)
}
});

View file

@ -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');
}
});

View file

@ -8,13 +8,13 @@
<td>Upvoted At</td>
</tr>
</thead>
{{#each upvotedPosts}}
{{#each posts}}
<tr>
<td><a href="{{pathFor route='post_page' _id=_id}}">{{title}}</a></td>
<td>{{formatDate votedAt "MM/DD/YYYY, HH:mm"}}</td>
</tr>
{{/each}}
{{#if hasMoreUpvotedPosts}}
{{#if hasMorePosts}}
<tr>
<td colspan="2">
<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 () {
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)
}
});