Added voting to comments.

This commit is contained in:
Tom Coleman 2012-09-18 09:29:31 +10:00
parent 85b09d702a
commit fcf8c4ae58
3 changed files with 31 additions and 6 deletions

View file

@ -120,6 +120,10 @@ Template.comment_item.rendered=function(){
} }
); );
// $(event.target).closest(".comment").addClass("queued"); // $(event.target).closest(".comment").addClass("queued");
},
'click .upvote': function(e) {
e.preventDefault();
Meteor.call('voteForComment', this._id);
} }
}; };

View file

@ -8,7 +8,7 @@ Template.post_item.events = {
, 'click .upvote-link': function(){ , 'click .upvote-link': function(){
console.log('upvote', this); console.log('upvote', this);
Meteor.call('voteForPost', this); Meteor.call('voteForPost', this._id);
} }
, 'click .share-link': function(e){ , 'click .share-link': function(e){

View file

@ -1,19 +1,40 @@
Meteor.methods({ Meteor.methods({
voteForPost: function(post){ voteForPost: function(postId){
console.log('voting for ' + post._id); console.log('voting for ' + postId);
var userId = this.userId(); var userId = this.userId();
if(!userId) return false; if(!userId) return false;
// atomically update the post's votes // atomically update the post's votes
var query = {_id: post._id, voters: {$ne: userId}}; var query = {_id: postId, voters: {$ne: userId}};
var update = {$push: {voters: userId}, $inc: {votes: 1}}; var update = {$push: {voters: userId}, $inc: {votes: 1}};
Posts.update(query, update); Posts.update(query, update);
if (!this.is_simulation) { if (!this.is_simulation) {
// now update the post's score // now update the post's score
post = Posts.findOne(post._id); post = Posts.findOne(postId);
Scoring.updateObject(post); Scoring.updateObject(post);
Posts.update(post._id, {$set: {score: post.score}}); Posts.update(postId, {$set: {score: post.score}});
}
return true;
},
// this is like the exact same code as above. Is there a way to refactor?
voteForComment: function(commentId) {
console.log('voting for ' + commentId);
var userId = this.userId();
if(!userId) return false;
// atomically update the comment's votes
var query = {_id: commentId, voters: {$ne: userId}};
var update = {$push: {voters: userId}, $inc: {votes: 1}};
Comments.update(query, update);
if (!this.is_simulation) {
// now update the comment's score
comment = Comments.findOne(commentId);
Scoring.updateObject(comment);
Comments.update(commentId, {$set: {score: comment.score}});
} }
return true; return true;