Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

For example in your panel:

Code Block

final AbstractDefaultAjaxBehavior behave = new AbstractDefaultAjaxBehavior() {
    protected void respond(final AjaxRequestTarget target) {
        target.add(new Label("foo", "Yeah I was just called from Javascript!"));
    }
};
add(behave);

...

Code Block
html
html
titleHTML

<script type="text/javascript" wicket:id="myScript">/* script will be rendered here */</script>
Code Block
java
java
titleJava

Label myScript = new Label("myScript", "callWicket();");
myScript.setEscapeModelStrings(false); // do not HTML escape JavaScript code
add(myScript);

...

If you add any class that extends AbstractDefaultAjaxBehavior to your page, wicket-ajax.js will be added to the header ofyour web page. wicket-ajax.js provides you with two basic methods to call your component:

Wicket 6.0+

You use Wicket.Ajax.get( )
and
Wicket.Ajax.post( ) in JavaScript to do an Ajax call to Wicket manually. In Wicket 6, AbstractDefaultAjaxBehavior also has a convenience method to generate a JavaScript function that performs the call: getCallbackFunction(...). This method can also generate functions that pass parameters from JavaScript to Wicket.

Older Versions

function wicketAjaxGet(url, successHandler, failureHandler, precondition, channel)
and
function wicketAjaxPost(url, body, successHandler, failureHandler, precondition, channel)

...

Code Block
java
java
titleJava

		String componentMarkupId = component.getMarkupId();
		String callbackUrl = behave.getCallbackUrl().toString();

		Map<String, Object> map = new HashMap<>();
		map.put( "callbackUrl", callbackUrl );
		map.put( "args", "Your Arguments Here" );
		map.put( "componentMarkupId", componentMarkupId );

		PackageTextTemplate ptt = new PackageTextTemplate( clazz, "resources/main.js" );
		OnDomReadyHeaderItem onDomReadyHeaderItem = OnDomReadyHeaderItem.forScript( ptt.asString( map ) );
Code Block
javascript
javascript
titleJavaScript - Fragment of resources/main.js

var wcall = Wicket.Ajax.get({ u: '${callbackUrl}' + '${args}' });

...

Code Block
javascript
javascript
titleJavaScript

function callWicket() {
   var wcall = wicketAjaxGet('$url$' + '$args$', function() { }, function() { });
}

...

Ok, this is actually quite ugly, but you get the optional arguments in the response method like this:

Code Block
titleJava

IRequestParameters params = RequestCycle.get().getRequest().getRequestParameters();

Or to retrieve a single parameter by its key:

Code Block
titleJava

String paramFoo = RequestCycle.get().getRequest().getRequestParameters().getParameterValue("foo");