comment_submit.js 998 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /**
  2. * @file
  3. *
  4. *
  5. * User: marand
  6. * Date: 06/09/15
  7. * Time: 22:05
  8. */
  9. Template.commentSubmit.onCreated(function () {
  10. Session.set('commentSubmitErrors', {});
  11. });
  12. Template.commentSubmit.helpers({
  13. errorMessage: function (field) {
  14. return Session.get('commentSubmitErrors')[field];
  15. },
  16. errorClass: function (field) {
  17. return !!Session.get('commentSubmitErrors')[field] ? 'has-error' : '';
  18. },
  19. });
  20. Template.commentSubmit.events({
  21. 'submit form': function (e, template) {
  22. e.preventDefault();
  23. var $body = $(e.target).find('[name=body]');
  24. var comment = {
  25. body: $body.val(),
  26. postId: template.data._id
  27. };
  28. var errors = {};
  29. if (!comment.body) {
  30. errors.body = 'Please write some content';
  31. return Session.set('commentSubmitErrors', errors);
  32. }
  33. Meteor.call('commentInsert', comment, function (error, commentId) {
  34. if (error) {
  35. throwError(error.reason);
  36. }
  37. else {
  38. $body.val('');
  39. }
  40. });
  41. }
  42. });