Skip to content

Instantly share code, notes, and snippets.

@sody
Created June 29, 2012 16:33
Show Gist options
  • Save sody/3019018 to your computer and use it in GitHub Desktop.
Save sody/3019018 to your computer and use it in GitHub Desktop.
Prevent Double Submit
package com.example.ui.mixins;
import org.apache.tapestry5.annotations.AfterRender;
import org.apache.tapestry5.annotations.InjectContainer;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.javascript.JavaScriptSupport;
/**
* This class represents fixin for form component that adds client-side logic to page that prevents double form
* submission. It will mark form as submitted on {@code Tapestry.FORM_PREPARE_FOR_SUBMIT_EVENT} client event and if it
* has already marked as submitted - will stop {@code Tapestry.FORM_PROCESS_SUBMIT_EVENT} event. After zone was updated
* with new content, our form will be marked as clear.
*
* @author Ivan Khalopik
*/
public class PreventDoubleSubmit {
@InjectContainer
private Form form;
@Inject
private JavaScriptSupport javascriptSupport;
/**
* Renders mixin's JavaScript.
*/
@AfterRender
void renderScript() {
javascriptSupport.addInitializerCall("preventDoubleSubmit", form.getClientId());
}
}
// PreventDoubleSubmit fixin script
Tapestry.Initializer.preventDoubleSubmit = function(spec) {
// find tapestry form
var tapestryForm = $(spec);
// bind event handler on prepare for submit
tapestryForm.observe(Tapestry.FORM_PREPARE_FOR_SUBMIT_EVENT, function() {
// stop submission if it is already in process
if (tapestryForm.preventSubmit) {
tapestryForm.stopObserving(Tapestry.FORM_PROCESS_SUBMIT_EVENT)
}
// mark form as in process
tapestryForm.preventSubmit = true;
});
// bind event handler on zone updated event
document.observe(Tapestry.ZONE_UPDATED_EVENT, function() {
// remove in process mark from form
tapestryForm.preventSubmit = false;
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment