Example usage for org.apache.wicket.ajax.attributes AjaxCallListener onComplete

List of usage examples for org.apache.wicket.ajax.attributes AjaxCallListener onComplete

Introduction

In this page you can find the example usage for org.apache.wicket.ajax.attributes AjaxCallListener onComplete.

Prototype

public AjaxCallListener onComplete(final CharSequence complete) 

Source Link

Document

Sets the JavaScript code that will be returned by #getCompleteHandler(Component) .

Usage

From source file:com.wicketinaction.HandlebarsButton.java

License:Apache License

@Override
protected void updateAjaxAttributes(AjaxRequestAttributes attributes) {
    super.updateAjaxAttributes(attributes);

    // let Wicket.Ajax/jQuery know that the result is JSON so it will parse it for you
    attributes.setDataType("json");

    // tell Wicket.Ajax to not try to process the Ajax response because it is not the normal <ajax-response>
    attributes.setWicketAjaxResponse(false);

    // register the onSuccess listener that will execute Handlebars logic
    AjaxCallListener listener = new AjaxCallListener() {
        @Override/*  w w  w . jav a  2  s  .com*/
        public CharSequence getSuccessHandler(Component component) {
            // returns an instance of JsonFunction, so Wicket will render it as is
            return onSuccessFunction;
        }
    };

    // the following handlers are not JsonFunction and they will be wrapped in JsonFunction by Wicket and the
    // appropriate parameters will be passed.

    // For the 'before' handler it looks like: function(attrs, jqXHR, settings){Wicket.Log.info('[Wicket Ajax 6 demo]: executing a before handler');}
    listener.onBefore("Wicket.Log.info('[Wicket Ajax 6 demo]: executing a before handler');");

    // For the 'complete' handler it looks like: function(attrs, jqXHR, textStatus){Wicket.Log.info('[Wicket Ajax 6 demo]: executing a complete handler');}
    listener.onComplete(
            "Wicket.Log.info('[Wicket Ajax 6 demo]: executing a complete handler. Status: ' + textStatus);");

    // change the return to 'false' and the Ajax call wont be executed at all.
    listener.onPrecondition("return true;");

    attributes.getAjaxCallListeners().add(listener);
}