use type=method for submit and edit forms

This commit is contained in:
Sacha Greif 2014-12-22 09:49:28 +09:00
parent e5c1b775af
commit 6fc3bb7092
7 changed files with 54 additions and 98 deletions

View file

@ -1,3 +1,6 @@
* Post submit and edit forms now submit to their respective methods directly.
* Removed `postSubmitRenderedCallbacks` and `postEditRenderedCallbacks`.
## v0.11.0 “AvatarScope”
* Added new `userCreatedCallbacks` callback hook.

View file

@ -1,7 +1,7 @@
<template name="post_edit">
<div class="grid grid-module">
{{> quickForm collection="Posts" doc=post id="editPostForm" template="telescope" label-class="control-label" input-col-class="controls"}}
{{> quickForm collection="Posts" doc=post id="editPostForm" template="telescope" label-class="control-label" input-col-class="controls" type="method" meteormethod="editPost"}}
</div>
<div class="grid grid-module">

View file

@ -1,46 +1,33 @@
AutoForm.hooks({
editPostForm: {
onSubmit: function(insertDoc, updateDoc, currentDoc) {
var updateObject = updateDoc;
var submit = this;
before: {
editPost: function(doc, template) {
// ------------------------------ Checks ------------------------------ //
var post = doc;
post._id = template.data.doc._id; // decorate with post _id
if (!Meteor.user()) {
flashMessage(i18n.t('you_must_be_logged_in'), "");
return false;
}
// ------------------------------ Checks ------------------------------ //
// ------------------------------ Callbacks ------------------------------ //
// run all post edit client callbacks on updateObject object successively
updateObject = postEditClientCallbacks.reduce(function(result, currentFunction) {
return currentFunction(result);
}, updateObject);
// ------------------------------ 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});
submit.done();
if (!Meteor.user()) {
flashMessage(i18n.t('you_must_be_logged_in'), "");
return false;
}
});
return false
// ------------------------------ Callbacks ------------------------------ //
// run all post edit client callbacks on post object successively
post = postEditClientCallbacks.reduce(function(result, currentFunction) {
return currentFunction(result);
}, post);
return post;
}
},
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);
onSuccess: function(operation, post, template) {
trackEvent("edit post", {'postId': post._id});
Router.go('post_page', {_id: post._id});
},
onError: function(operation, error, template) {
@ -49,16 +36,10 @@ AutoForm.hooks({
clearSeenMessages();
}
// 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) {}
}
});
// delete link
Template[getTemplate('post_edit')].events({
'click .delete-link': function(e){
var post = this.post;

View file

@ -1,7 +1,7 @@
<template name="post_submit">
<div class="grid grid-module">
{{> quickForm collection="Posts" id="submitPostForm" template="telescope" label-class="control-label" input-col-class="controls"}}
{{> quickForm collection="Posts" id="submitPostForm" template="telescope" label-class="control-label" input-col-class="controls" type="method" meteormethod="submitPost"}}
</div>
</template>

View file

@ -1,51 +1,35 @@
AutoForm.hooks({
submitPostForm: {
onSubmit: function(insertDoc, updateDoc, currentDoc) {
var properties = insertDoc;
var submit = this;
before: {
submitPost: function(doc, template) {
// ------------------------------ Checks ------------------------------ //
var post = doc;
if (!Meteor.user()) {
flashMessage(i18n.t('you_must_be_logged_in'), 'error');
return false;
}
// ------------------------------ Checks ------------------------------ //
// ------------------------------ 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 ------------------------------ //
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});
if (post.status === STATUS_PENDING) {
flashMessage(i18n.t('thanks_your_post_is_awaiting_approval'), 'success');
}
Router.go('post_page', {_id: post._id});
submit.done();
if (!Meteor.user()) {
flashMessage(i18n.t('you_must_be_logged_in'), 'error');
return false;
}
});
return false
// ------------------------------ Callbacks ------------------------------ //
// run all post submit client callbacks on properties object successively
post = postSubmitClientCallbacks.reduce(function(result, currentFunction) {
return currentFunction(result);
}, post);
return post;
}
},
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(post)
// trackEvent("new post", {'postId': post._id});
// if(post.status === STATUS_PENDING)
// throwError('Thanks, your post is awaiting approval.');
// Router.go('/posts/'+post._id);
onSuccess: function(operation, post, template) {
trackEvent("new post", {'postId': post._id});
if (post.status === STATUS_PENDING) {
flashMessage(i18n.t('thanks_your_post_is_awaiting_approval'), 'success');
}
Router.go('post_page', {_id: post._id});
},
onError: function(operation, error, template) {
@ -57,13 +41,6 @@ AutoForm.hooks({
Router.go('/posts/'+dupePostId);
}
}
// 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) {}
}
});

View file

@ -455,11 +455,10 @@ Meteor.methods({
return submitPost(post);
},
editPost: function (postId, updateObject) {
editPost: function (post, updateObject) {
var user = Meteor.user();
// console.log(updateObject)
var postId = post._id;
// ------------------------------ Checks ------------------------------ //
@ -474,8 +473,6 @@ Meteor.methods({
return currentFunction(result);
}, updateObject);
console.log(updateObject)
// ------------------------------ Update ------------------------------ //
Posts.update(postId, updateObject);

View file

@ -224,15 +224,13 @@ postMeta = [
]
// ------------------------------ Callbacks ------------------------------ //
postSubmitRenderedCallbacks = [];
postSubmitClientCallbacks = [];
postSubmitMethodCallbacks = [];
postAfterSubmitMethodCallbacks = [];
postEditRenderedCallbacks = [];
postEditClientCallbacks = [];
postEditMethodCallbacks = []; // not used yet
postAfterEditMethodCallbacks = []; // not used yet
postEditClientCallbacks = []; // loops over post object
postEditMethodCallbacks = []; // loops over modifier (i.e. "{$set: {foo: bar}}") object
postAfterEditMethodCallbacks = []; // loops over modifier object
commentSubmitRenderedCallbacks = [];
commentSubmitClientCallbacks = [];