image_share.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // this is image_share.js
  2. Images = new Mongo.Collection("images");
  3. if (Meteor.isClient) {
  4. Template.images.helpers({
  5. images: Images.find({}, { sort: { createdOn: -1, rating: -1 }}),
  6. image_id: function () {
  7. return "rating-" + this._id;
  8. }
  9. });
  10. Template.images.events({
  11. "click .js-image": function (event) {
  12. $(event.target).css("width", "50px");
  13. },
  14. "click .js-del-image": function (event) {
  15. let imageId = this._id;
  16. console.log(imageId);
  17. // use jquery to hide the image component
  18. // then remove it at the end of the animation
  19. $("#" + imageId).hide("slow", function () {
  20. Images.remove({ _id: imageId });
  21. });
  22. },
  23. "click .js-rate-image": function (event) {
  24. let rating = $(event.currentTarget).data("userrating");
  25. let imageId = this.id.substring(7); // "rating-".length = 7.
  26. console.log(imageId);
  27. Images.update({ _id: imageId },
  28. { $set: { rating: rating }}
  29. );
  30. },
  31. "click .js-show-image-form": function (event) {
  32. $("#image_add_form").modal("show");
  33. }
  34. });
  35. Template.image_add_form.events({
  36. 'submit .js-add-image': function (event) {
  37. let img_src = event.target.img_src.value;
  38. let img_alt = event.target.img_alt.value;
  39. console.log("src: " + img_src + " alt: " + img_alt);
  40. Images.insert({
  41. img_src: img_src,
  42. img_alt: img_alt,
  43. createdOn: new Date()
  44. });
  45. $("#image_add_form").modal('show');
  46. return false;
  47. }
  48. });
  49. }