|
@@ -14,15 +14,22 @@ Template.website_item.events({
|
|
|
console.log("Up voting website with id " + websiteId);
|
|
|
// Put the code in here to add a vote to a website!
|
|
|
const userId = Meteor.userId();
|
|
|
- const modifiers = {
|
|
|
- $addToSet: {
|
|
|
- plus: userId
|
|
|
- },
|
|
|
- $pull: {
|
|
|
- minus: userId
|
|
|
- }
|
|
|
- };
|
|
|
- Websites.update({ _id: websiteId }, modifiers);
|
|
|
+ let modifiers = {};
|
|
|
+ let increments = {}
|
|
|
+ if (!_.contains(this.plus, userId)) {
|
|
|
+ modifiers.$addToSet = { plus: userId };
|
|
|
+ increments.plusScore = 1;
|
|
|
+ }
|
|
|
+ if (_.contains(this.minus, userId)) {
|
|
|
+ modifiers.$pull = { minus: userId };
|
|
|
+ increments.minusScore = -1;
|
|
|
+ }
|
|
|
+ if (!_.isEmpty(increments)) {
|
|
|
+ modifiers.$inc = increments;
|
|
|
+ }
|
|
|
+ if (!_.isEmpty(modifiers)) {
|
|
|
+ Websites.update({ _id: websiteId }, modifiers);
|
|
|
+ }
|
|
|
|
|
|
// Prevent the button from reloading the page.
|
|
|
return false;
|
|
@@ -36,15 +43,22 @@ Template.website_item.events({
|
|
|
|
|
|
// Put the code in here to remove a vote from a website!
|
|
|
const userId = Meteor.userId();
|
|
|
- const modifiers = {
|
|
|
- $addToSet: {
|
|
|
- minus: userId
|
|
|
- },
|
|
|
- $pull: {
|
|
|
- plus: userId
|
|
|
- }
|
|
|
- };
|
|
|
- Websites.update({ _id: websiteId }, modifiers);
|
|
|
+ let modifiers = {};
|
|
|
+ let increments = {}
|
|
|
+ if (!_.contains(this.minus, userId)) {
|
|
|
+ modifiers.$addToSet = { minus: userId };
|
|
|
+ increments.minusScore = 1;
|
|
|
+ }
|
|
|
+ if (_.contains(this.plus, userId)) {
|
|
|
+ modifiers.$pull = { plus: userId };
|
|
|
+ increments.plusScore = -1;
|
|
|
+ }
|
|
|
+ if (!_.isEmpty(increments)) {
|
|
|
+ modifiers.$inc = increments;
|
|
|
+ }
|
|
|
+ if (!_.isEmpty(modifiers)) {
|
|
|
+ Websites.update({ _id: websiteId }, modifiers);
|
|
|
+ }
|
|
|
|
|
|
// Prevent the button from reloading the page
|
|
|
return false;
|