Created
September 23, 2010 22:19
-
-
Save jonnywray/594504 to your computer and use it in GitHub Desktop.
Wicket StopFormSubmitBehaviour: allows non Ajax form submits (e.g. return press) to go via Ajax route
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.wicket.Component; | |
import org.apache.wicket.behavior.AbstractBehavior; | |
import org.apache.wicket.markup.html.IHeaderResponse; | |
/** | |
* Behavior to allow non ajax form submits (eg press return) to go via the Ajax | |
* submit route. Note that it requires the prototype.js javascript library. | |
* | |
* @author <a href="http://richard-wilkinson.co.uk/2008/04/05/how-to-stop-non-ajax-form-submits-in-wicket/">Richard Wilkinson</a> | |
* | |
*/ | |
public class StopFormSubmitBehaviour extends AbstractBehavior { | |
private static final long serialVersionUID = 5738925186056794947L; | |
private Component submitButton; | |
private Component form; | |
public StopFormSubmitBehaviour(Component submitButton){ | |
this.submitButton = submitButton; | |
submitButton.setOutputMarkupId(true); | |
} | |
@Override | |
public void bind(Component component) { | |
super.bind(component); | |
component.setOutputMarkupId(true); | |
this.form = component; | |
} | |
@Override | |
public void renderHead(IHeaderResponse response) { | |
super.renderHead(response); | |
response.renderOnDomReadyJavascript(getScript()); | |
} | |
protected String getScript(){ | |
StringBuilder js = new StringBuilder(); | |
js.append("$('").append(form.getMarkupId()).append("').observe('submit', function(e){Event.stop(e); $('") | |
.append(submitButton.getMarkupId()).append("').onclick(null);});"); | |
return js.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment