website_item.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. const modifiers = {
  16. $addToSet: {
  17. plus: userId
  18. },
  19. $pull: {
  20. minus: userId
  21. }
  22. };
  23. Websites.update({ _id: websiteId }, modifiers);
  24. // Prevent the button from reloading the page.
  25. return false;
  26. },
  27. "click .js-downvote": function () {
  28. // example of how you can access the id for the website in the database
  29. // (this is the data context for the template)
  30. const websiteId = this._id;
  31. console.log("Down voting website with id " + websiteId);
  32. // Put the code in here to remove a vote from a website!
  33. const userId = Meteor.userId();
  34. const modifiers = {
  35. $addToSet: {
  36. minus: userId
  37. },
  38. $pull: {
  39. plus: userId
  40. }
  41. };
  42. Websites.update({ _id: websiteId }, modifiers);
  43. // Prevent the button from reloading the page
  44. return false;
  45. }
  46. });
  47. Template.website_item.helpers({
  48. upVoted: function () {
  49. return _.contains(this.plus, Meteor.userId()) ? "btn-success" : "";
  50. },
  51. downVoted: function () {
  52. return _.contains(this.minus, Meteor.userId()) ? "btn-danger" : "";
  53. }
  54. });