post_edit.js 1.2 KB

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