12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- /**
- * Client code for the website_item template.
- */
- /**
- * Template events
- */
- Template.website_item.events({
- "click .js-upvote": function () {
- // Example of how you can access the id for the website in the database
- // (this is the data context for the template)
- const websiteId = this._id;
- console.log("Up voting website with id " + websiteId);
- // Put the code in here to add a vote to a website!
- const userId = Meteor.userId();
- 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;
- },
- "click .js-downvote": function () {
- // example of how you can access the id for the website in the database
- // (this is the data context for the template)
- const websiteId = this._id;
- console.log("Down voting website with id " + websiteId);
- // Put the code in here to remove a vote from a website!
- const userId = Meteor.userId();
- 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;
- }
- });
- Template.website_item.helpers({
- upVoted: function () {
- return _.contains(this.plus, Meteor.userId()) ? "btn-success" : "";
- },
- downVoted: function () {
- return _.contains(this.minus, Meteor.userId()) ? "btn-danger" : "";
- }
- });
|