comment_submit.js 916 B

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