mirror of
https://github.com/vale981/Vulcan
synced 2025-03-06 10:01:40 -05:00
Added voting to comments.
This commit is contained in:
parent
85b09d702a
commit
fcf8c4ae58
3 changed files with 31 additions and 6 deletions
|
@ -120,6 +120,10 @@ Template.comment_item.rendered=function(){
|
|||
}
|
||||
);
|
||||
// $(event.target).closest(".comment").addClass("queued");
|
||||
},
|
||||
'click .upvote': function(e) {
|
||||
e.preventDefault();
|
||||
Meteor.call('voteForComment', this._id);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -8,7 +8,7 @@ Template.post_item.events = {
|
|||
|
||||
, 'click .upvote-link': function(){
|
||||
console.log('upvote', this);
|
||||
Meteor.call('voteForPost', this);
|
||||
Meteor.call('voteForPost', this._id);
|
||||
}
|
||||
|
||||
, 'click .share-link': function(e){
|
||||
|
|
31
lib/vote.js
31
lib/vote.js
|
@ -1,19 +1,40 @@
|
|||
Meteor.methods({
|
||||
voteForPost: function(post){
|
||||
console.log('voting for ' + post._id);
|
||||
voteForPost: function(postId){
|
||||
console.log('voting for ' + postId);
|
||||
var userId = this.userId();
|
||||
if(!userId) return false;
|
||||
|
||||
// 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}};
|
||||
Posts.update(query, update);
|
||||
|
||||
if (!this.is_simulation) {
|
||||
// now update the post's score
|
||||
post = Posts.findOne(post._id);
|
||||
post = Posts.findOne(postId);
|
||||
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;
|
||||
|
|
Loading…
Add table
Reference in a new issue