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

48 lines
1.4 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;
var instance = this;
instance.commentsShown = new ReactiveVar(5);
instance.comments = new ReactiveVar({});
2014-12-18 10:36:41 +09:00
this.autorun(function () {
// get parameters
var limit = instance.commentsShown.get();
// subscribe
instance.subscription = Meteor.subscribe('userComments', user._id, limit);
// set cursor
instance.comments.set(Comments.find({userId: user._id}, {limit: limit}));
});
2014-12-08 14:53:26 +09:00
};
Template[getTemplate('userComments')].helpers({
comments: function () {
var comments = Template.instance().comments.get();
2014-12-08 14:53:26 +09:00
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 Template.instance().comments.get().count() >= Template.instance().commentsShown.get();
2014-12-08 14:53:26 +09:00
}
});
Template[getTemplate('userComments')].events({
'click .comments-more': function (e) {
e.preventDefault();
var commentsShown = Template.instance().commentsShown.get();
Template.instance().commentsShown.set(commentsShown+5);
2014-12-08 14:53:26 +09:00
}
});