2014-11-28 11:19:55 +09:00
|
|
|
AutoForm.hooks({
|
2014-11-29 10:25:11 +09:00
|
|
|
submitPostForm: {
|
2014-11-28 11:19:55 +09:00
|
|
|
onSubmit: function(insertDoc, updateDoc, currentDoc) {
|
2014-11-28 15:18:55 +09:00
|
|
|
|
|
|
|
var properties = insertDoc;
|
|
|
|
var submit = this;
|
|
|
|
|
|
|
|
// ------------------------------ Checks ------------------------------ //
|
|
|
|
|
|
|
|
if (!Meteor.user()) {
|
2014-12-06 17:34:08 +09:00
|
|
|
flashMessage(i18n.t('you_must_be_logged_in'), 'error');
|
2014-11-28 15:18:55 +09:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------ Callbacks ------------------------------ //
|
|
|
|
|
|
|
|
// run all post submit client callbacks on properties object successively
|
|
|
|
properties = postSubmitClientCallbacks.reduce(function(result, currentFunction) {
|
|
|
|
return currentFunction(result);
|
|
|
|
}, properties);
|
|
|
|
|
|
|
|
// console.log(properties)
|
|
|
|
|
|
|
|
// ------------------------------ Insert ------------------------------ //
|
2014-11-29 10:25:11 +09:00
|
|
|
Meteor.call('submitPost', properties, function(error, post) {
|
|
|
|
if(error){
|
|
|
|
submit.done(error);
|
|
|
|
}else{
|
|
|
|
// note: find a way to do this in onSuccess instead?
|
|
|
|
trackEvent("new post", {'postId': post._id});
|
2014-12-03 00:06:00 -08:00
|
|
|
if (post.status === STATUS_PENDING) {
|
2014-12-06 17:34:08 +09:00
|
|
|
flashMessage(i18n.t('thanks_your_post_is_awaiting_approval'), 'success');
|
2014-12-03 00:06:00 -08:00
|
|
|
}
|
|
|
|
Router.go('post_page', {_id: post._id});
|
2014-11-29 10:25:11 +09:00
|
|
|
submit.done();
|
|
|
|
}
|
|
|
|
});
|
2014-11-28 15:18:55 +09:00
|
|
|
|
|
|
|
return false
|
2014-11-28 11:19:55 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
onSuccess: function(operation, result, template) {
|
2014-11-28 15:18:55 +09:00
|
|
|
// not used right now because I can't find a way to pass the "post" object to this callback
|
|
|
|
// console.log(post)
|
|
|
|
// trackEvent("new post", {'postId': post._id});
|
|
|
|
// if(post.status === STATUS_PENDING)
|
|
|
|
// throwError('Thanks, your post is awaiting approval.');
|
|
|
|
// Router.go('/posts/'+post._id);
|
2014-12-03 00:06:00 -08:00
|
|
|
},
|
2014-11-28 11:19:55 +09:00
|
|
|
|
|
|
|
onError: function(operation, error, template) {
|
2014-12-06 17:49:29 +09:00
|
|
|
flashMessage(error.message.split('|')[0], 'error'); // workaround because error.details returns undefined
|
2014-12-06 17:41:15 +09:00
|
|
|
clearSeenMessages();
|
2014-11-28 15:18:55 +09:00
|
|
|
// $(e.target).removeClass('disabled');
|
|
|
|
if (error.error == 603) {
|
|
|
|
var dupePostId = error.reason.split('|')[1];
|
|
|
|
Router.go('/posts/'+dupePostId);
|
|
|
|
}
|
2014-07-03 13:15:23 +09:00
|
|
|
}
|
2014-11-28 11:19:55 +09:00
|
|
|
|
|
|
|
// Called at the beginning and end of submission, respectively.
|
|
|
|
// This is the place to disable/enable buttons or the form,
|
|
|
|
// show/hide a "Please wait" message, etc. If these hooks are
|
|
|
|
// not defined, then by default the submit button is disabled
|
|
|
|
// during submission.
|
|
|
|
// beginSubmit: function(formId, template) {},
|
|
|
|
// endSubmit: function(formId, template) {}
|
2012-10-10 08:32:49 +09:00
|
|
|
}
|
2014-11-28 15:18:55 +09:00
|
|
|
});
|