post_submit.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /**
  2. * @file
  3. *
  4. *
  5. * User: marand
  6. * Date: 31/08/15
  7. * Time: 20:47
  8. */
  9. Template.postSubmit.events({
  10. 'submit form': function (e) {
  11. e.preventDefault();
  12. var post = {
  13. url: $(e.target).find('[name=url]').val(),
  14. title: $(e.target).find('[name=title]').val()
  15. };
  16. var errors = validatePost(post);
  17. if (errors.title || errors.url) {
  18. Session.set('postSubmitErrors', errors);
  19. // We return to abort the helper, not for anyone using the result.
  20. return;
  21. }
  22. Meteor.call('postInsert', post, function (error, result) {
  23. // Display the error to the user and abort.
  24. if (error) {
  25. return throwError(error.reason);
  26. }
  27. if (result.postExists) {
  28. throwError("This link has already been posted");
  29. }
  30. Router.go('postPage', { _id: result._id });
  31. });
  32. }
  33. });
  34. Template.postSubmit.onCreated(function () {
  35. Session.set('postSubmitErrors', {});
  36. });
  37. Template.postSubmit.helpers({
  38. errorMessage: function (field) {
  39. return Session.get('postSubmitErrors')[field];
  40. },
  41. errorClass: function (field) {
  42. return Session.get('postSubmitErrors')[field] ? 'has-error' : '';
  43. }
  44. });