/** * @file * * * User: marand * Date: 31/08/15 * Time: 20:47 */ Template.postSubmit.events({ 'submit form': function (e) { e.preventDefault(); var post = { url: $(e.target).find('[name=url]').val(), title: $(e.target).find('[name=title]').val() }; var errors = validatePost(post); if (errors.title || errors.url) { Session.set('postSubmitErrors', errors); // We return to abort the helper, not for anyone using the result. return; } Meteor.call('postInsert', post, function (error, result) { // Display the error to the user and abort. if (error) { Errors.throw(error.reason); } if (result.postExists) { Errors.throw("This link has already been posted"); } Router.go('postPage', { _id: result._id }); }); } }); Template.postSubmit.onCreated(function () { Session.set('postSubmitErrors', {}); }); Template.postSubmit.helpers({ errorMessage: function (field) { return Session.get('postSubmitErrors')[field]; }, errorClass: function (field) { return Session.get('postSubmitErrors')[field] ? 'has-error' : ''; } });