Vulcan/client/views/users/profile/user_comments.js

34 lines
1.1 KiB
JavaScript
Raw Normal View History

2014-12-08 14:53:26 +09:00
Template[getTemplate('userComments')].created = function () {
Session.set('commentsShown', 5);
var user = this.data;
Tracker.autorun(function () {
coreSubscriptions.subscribe('userComments', user._id, Session.get('commentsShown'));
});
2014-12-08 14:53:26 +09:00
};
Template[getTemplate('userComments')].helpers({
comments: function () {
var comments = Comments.find({userId: this._id}, {limit: Session.get('commentsShown')});
if(!!comments){
// extend comments with each commented post
var extendedComments = comments.map(function (comment) {
var post = Posts.findOne(comment.postId);
if(post) // post might not be available anymore
comment.postTitle = post.title;
return comment;
});
return extendedComments;
}
},
hasMoreComments: function () {
return Comments.find({userId: this._id}).count() >= Session.get('commentsShown');
2014-12-08 14:53:26 +09:00
}
});
Template[getTemplate('userComments')].events({
'click .comments-more': function (e) {
e.preventDefault();
var commentsShown = Session.get('commentsShown');
Session.set('commentsShown', commentsShown + 10);
}
});