For a new project we are using a complex external JavaScript library which needs some parameters for initialization. This parameter values are only known at runtime and therefore we wanted to build some sort of AJAX mechanism for the JavaScript to load the parameters when needed.
I tried to achieve this by the following mechanisms:
PageThatContainsTheJavaScript.java
public GraphPanel(String id) {
add(new AbstractDefaultAjaxBehavior() {
protected void respond(final AjaxRequestTarget target) {
final RequestCycle requestCycle = RequestCycle.get();
TextRequestHandler textRequestHandler = new TextRequestHandler("text/plain", "UTF-8", content.toString()); //content - the parameter value
requestCycle.scheduleRequestHandlerAfterCurrent(textRequestHandler);
}
});
add(new Label("ajaxtest", ""));
}
@Override
protected void onBeforeRender() {
super.onBeforeRender();
String callbackUrl = behave.getCallbackUrl().toString();
String callbackFunction = String.format("Wicket.Ajax.get({'u':'%s'});",callbackUrl);
replace(new Label("ajaxtest", callbackFunction));
}
The label "ajaxtest" is just a way to conveniently access the generated callback url for testing purposes.
PageThatContainsTheJavaScript.html
function externalApplicationStart() {
var parameter = Wicket.Ajax.get({'u':"'"+document.getElementById('ajaxtest').innerHTML+"'"});
var application = new demo.yfiles.layout.modules.LayoutModulesDemo(parameter);
// arbitrary init code
application.start();
});
I didn't find a really good tutorial on this issues, so it is possible, that there is something wrong with that attempt, although I guess that the overall direction is correct.
My problem is, that as long as the Wicket.ajax.get line is present, the JavaScript function hangs. When I comment the line and pass a hardcoded default parameter, the application starts. What am I doing wrong and how can I get the String parameter from Wicket to the JavaScript function?
A functional solution I found is passing the parameter value as content of a hidden HTML tag (like <span id='...' style='display:none;'>parameterValue</span>), but in the future it might be neccessary to pass even longer values to the JavaScript (like XML content) and I don't want to mess the generated HTML up with all those non-content-values.
//edit: The generated ajaxtest-HTML-Tag looks like this:
<span wicket:id="ajaxtest" id="ajaxtest">
Wicket.Ajax.get({'u':'./.?0-1.IBehaviorListener.0-graphContainer-graphControl'});
</span>
Trying to execute Wicket.Ajax.get({'u':'./.?0-1.IBehaviorListener.0-graphContainer-graphControl'}); in the console only results in undefined.
Also I am not sure where that second dot in the URL comes from (./.?0...), but trying the same with ./?0... results in the same.