website_item.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /**
  2. * Client code for the website_item template.
  3. */
  4. /**
  5. * Template events
  6. */
  7. Template.website_item.events({
  8. "click .js-upvote": function () {
  9. // Example of how you can access the id for the website in the database
  10. // (this is the data context for the template)
  11. const websiteId = this._id;
  12. console.log("Up voting website with id " + websiteId);
  13. // Put the code in here to add a vote to a website!
  14. const userId = Meteor.userId();
  15. let modifiers = {};
  16. let increments = {}
  17. if (!_.contains(this.plus, userId)) {
  18. modifiers.$addToSet = { plus: userId };
  19. increments.plusScore = 1;
  20. }
  21. if (_.contains(this.minus, userId)) {
  22. modifiers.$pull = { minus: userId };
  23. increments.minusScore = -1;
  24. }
  25. if (!_.isEmpty(increments)) {
  26. modifiers.$inc = increments;
  27. }
  28. if (!_.isEmpty(modifiers)) {
  29. Websites.update({ _id: websiteId }, modifiers);
  30. }
  31. // Prevent the button from reloading the page.
  32. return false;
  33. },
  34. "click .js-downvote": function () {
  35. // example of how you can access the id for the website in the database
  36. // (this is the data context for the template)
  37. const websiteId = this._id;
  38. console.log("Down voting website with id " + websiteId);
  39. // Put the code in here to remove a vote from a website!
  40. const userId = Meteor.userId();
  41. let modifiers = {};
  42. let increments = {}
  43. if (!_.contains(this.minus, userId)) {
  44. modifiers.$addToSet = { minus: userId };
  45. increments.minusScore = 1;
  46. }
  47. if (_.contains(this.plus, userId)) {
  48. modifiers.$pull = { plus: userId };
  49. increments.plusScore = -1;
  50. }
  51. if (!_.isEmpty(increments)) {
  52. modifiers.$inc = increments;
  53. }
  54. if (!_.isEmpty(modifiers)) {
  55. Websites.update({ _id: websiteId }, modifiers);
  56. }
  57. // Prevent the button from reloading the page
  58. return false;
  59. }
  60. });
  61. Template.website_item.helpers({
  62. upVoted: function () {
  63. return _.contains(this.plus, Meteor.userId()) ? "btn-success" : "";
  64. },
  65. downVoted: function () {
  66. return _.contains(this.minus, Meteor.userId()) ? "btn-danger" : "";
  67. }
  68. });