javaajaxwickethtml5-historywicket-7

Wicket AjaxEventBehavior with History API


I am using Wicket 7.x with the History API. I am currently trying to wire up an AjaxEventBehavior for "onpopstate". I have code that adds an object whenever I do history.pushState, my problem is I can't access this extra information from the Java side with onEvent.

Sample pushState:

target.appendJavaScript("history.pushState({query: '" + 
searchResults.getQueryString() + "'}, null, window.location.pathname);");

EventBehavior:

this.getPage().add(new AjaxEventBehavior("onpopstate")
{     
     @Override
     protected void onEvent(AjaxRequestTarget target)
     {
         //code to get query defined in pushState
     }
});

updateAjaxAttributes is not a solution because it is only called when the page is rendered. I need to send this extra data when popstate is called.


Solution

  • updateAjaxAttributes can actually help you. AjaxRequestAttributes lets you specify also dynamic extra attributes (see here for more info). These parameters are dynamically calculated on client side before AjaxEventBehavior is triggered and are sent as request parameters.