Vulcan/client/views/posts/post_edit.js

80 lines
2.6 KiB
JavaScript
Raw Normal View History

2014-11-29 10:25:11 +09:00
AutoForm.hooks({
editPostForm: {
onSubmit: function(insertDoc, updateDoc, currentDoc) {
2014-11-29 10:25:11 +09:00
var updateObject = updateDoc;
var submit = this;
2012-09-13 11:05:49 +09:00
2014-11-29 10:25:11 +09:00
// ------------------------------ Checks ------------------------------ //
2013-10-09 12:39:05 +09:00
2014-11-29 10:25:11 +09:00
if (!Meteor.user()) {
flashMessage(i18n.t('you_must_be_logged_in'), "");
2014-11-29 10:25:11 +09:00
return false;
}
2014-07-03 13:15:23 +09:00
2014-11-29 10:25:11 +09:00
// ------------------------------ Callbacks ------------------------------ //
2014-11-29 10:25:11 +09:00
// run all post edit client callbacks on updateObject object successively
2014-11-29 11:14:44 +09:00
updateObject = postEditClientCallbacks.reduce(function(result, currentFunction) {
return currentFunction(result);
}, updateObject);
2014-07-03 13:15:23 +09:00
2014-11-29 10:25:11 +09:00
// ------------------------------ Update ------------------------------ //
Meteor.call('editPost', currentDoc._id, updateObject, function(error, post) {
if(error){
submit.done(error);
}else{
// note: find a way to do this in onSuccess instead?
trackEvent("edit post", {'postId': post._id});
Router.go('post_page', {_id: post._id});
2014-11-29 10:25:11 +09:00
submit.done();
}
});
2014-11-29 10:25:11 +09:00
return false
},
onSuccess: function(operation, result, template) {
// not used right now because I can't find a way to pass the "post" object to this callback
// console.log(result)
// trackEvent("new post", {'postId': post._id});
// if(post.status === STATUS_PENDING)
// throwError('Thanks, your post is awaiting approval.');
// Router.go('/posts/'+post._id);
},
2014-11-29 10:25:11 +09:00
onError: function(operation, error, template) {
console.log(error)
flashMessage(error.reason.split('|')[0], "error"); // workaround because error.details returns undefined
clearSeenMessages();
2013-01-13 09:13:25 +09:00
}
2014-11-29 10:25:11 +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) {}
}
2014-12-08 16:10:32 +09:00
});
Template[getTemplate('post_edit')].events({
'click .delete-link': function(e){
var post = this.post;
e.preventDefault();
if(confirm("Are you sure?")){
Router.go("/");
Meteor.call("deletePostById", post._id, function(error) {
if (error) {
console.log(error);
flashMessage(error.reason, 'error');
} else {
flashMessage(i18n.t('your_post_has_been_deleted'), 'success');
}
});
}
}
2014-12-03 09:10:14 +09:00
});