Skip to content

Instantly share code, notes, and snippets.

@kemsakurai
Created March 11, 2017 03:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kemsakurai/3ba1d1120572d284c2910454ada71122 to your computer and use it in GitHub Desktop.
Save kemsakurai/3ba1d1120572d284c2910454ada71122 to your computer and use it in GitHub Desktop.
StatelessAjaxPreventSubmitBehavior を作成しましたが、`AjaxCallListener` がStatefull のためStatelessにはならなかった。
import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.attributes.AjaxCallListener;
import org.apache.wicket.ajax.attributes.AjaxRequestAttributes;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.wicketstuff.stateless.behaviors.StatelessAjaxEventBehavior;
/**
* StatelessAjaxPreventSubmitBehavior
*/
public class StatelessAjaxPreventSubmitBehavior extends StatelessAjaxEventBehavior {
/**
* Construct..
*/
public StatelessAjaxPreventSubmitBehavior() {
super("keydown");
}
protected void updateAjaxAttributes(AjaxRequestAttributes attributes) {
super.updateAjaxAttributes(attributes);
Component component = this.getComponent();
if (!(component instanceof TextField)) {
attributes.setChildSelector("input");
}
AjaxCallListener listener = new AjaxCallListener();
listener.onPrecondition("if (Wicket.Event.keyCode(attrs.event) === 13) {attrs.event.preventDefault();} return false;");
attributes.getAjaxCallListeners().add(listener);
}
@Override
protected PageParameters getPageParameters() {
return null;
}
@Override
protected void onEvent(AjaxRequestTarget ajaxRequestTarget) {
ajaxRequestTarget.appendJavaScript("alert('test');");
}
}
@kemsakurai
Copy link
Author

何か別の方法があるのかもしれません。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment