post_edit.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /**
  2. * @file
  3. *
  4. *
  5. * User: marand
  6. * Date: 02/09/15
  7. * Time: 08:43
  8. */
  9. Template.postEdit.events({
  10. 'submit form': function (e) {
  11. e.preventDefault();
  12. var currentPostId = this._id;
  13. var postProperties = {
  14. url: $(e.target).find('[name=url]').val(),
  15. title: $(e.target).find('[name=title]').val()
  16. };
  17. var errors = validatePost(postProperties);
  18. if (errors.title || errors.url) {
  19. Session.set('postEditErrors', errors);
  20. return;
  21. }
  22. Posts.update(currentPostId, { $set: postProperties }, function (error) {
  23. // Display the error to the user and abort.
  24. if (error) {
  25. Errors.throw(error.reason);
  26. }
  27. else {
  28. Router.go('postPage', { _id: currentPostId });
  29. }
  30. });
  31. },
  32. 'click .delete': function (e) {
  33. e.preventDefault();
  34. if (confirm("Delete this post ?")) {
  35. var currentPostId = this._id;
  36. Posts.remove(currentPostId);
  37. Router.go('postsList');
  38. }
  39. }
  40. });
  41. Template.postEdit.onCreated(function () {
  42. Session.set('postEditErrors', {});
  43. });
  44. Template.postEdit.helpers({
  45. errorMessage: function (field) {
  46. return Session.get('postEditErrors')[field];
  47. },
  48. errorClass: function (field) {
  49. return Session.get('postEditErrors')[field] ? 'has-error' : '';
  50. }
  51. });