website_item.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. postDate: function () {
  69. return this.postDate ? this.postDate : "Application launch";
  70. },
  71. poster: function () {
  72. return this.poster && this.poster.name ? this.poster.name : "Application setup";
  73. },
  74. minusVotes: function () {
  75. return this.minusScore ? this.minusScore : 0;
  76. },
  77. plusVotes: function () {
  78. return this.plusScore ? this.plusScore : 0;
  79. },
  80. score: function () {
  81. const plus = this.plusScore ? this.plusScore : 0;
  82. const minus = this.minusScore ? this.minusScore : 0;
  83. return plus - minus;
  84. }
  85. });