Search code examples
jquerywicketbehaviorjquery-callback

Wicket AjaxRequestTarget of jQuery callback does not Component


I'm trying to implement an jQuery AJAX callback to receive an event of a third party JavaScript library.

And it nearly works! :)

The jQuery AJAX post is receiver at the server side. The callback URL is generated by an AbstractDefaultAjaxBehavior, which has been added to a Panel. On respond method of the Behavior I added a child component of the Panel to the AjaxRequestTarget. But this does not repaint the component. No exception is thrown, output oft markupId is set.

Any idea? Any idea where I can find a wicket component doing something similar in GitHub, etc.


Solution

  • My solution based on the comments above. Simple Label to update via AjaxCallback:

    
        public class HomePage extends WebPage {
    
            private static final long serialVersionUID = 1L;
    
            private String value = "Hello World";
    
            public HomePage(final PageParameters parameters) {
                super(parameters);
    
                final Label label = new Label("label", new PropertyModel(HomePage.this, "value"));
                label.setOutputMarkupId(true);
                add(label);
    
                label.add(new DemoCallback(){
                    @Override
                    protected void onCallback(String fromClient, AjaxRequestTarget target) {
                        value = fromClient;
                        target.add(label);
                    }
                }); 
            }
        }
    
    

    The behavior which render the callback:

        public class DemoCallback extends AbstractDefaultAjaxBehavior {
    
            @Override
            public void renderHead(Component component, IHeaderResponse response) {
                super.renderHead(component, response);
    
                StringBuffer script = new StringBuffer();
                script.append("alert('Callback will follow!');\n");
                script.append("var dataForServer='From client with love.';\n");
                script.append(getCallbackFunctionBody(CallbackParameter
                        .explicit("dataForServer")));
    
                response.render(OnEventHeaderItem.forScript(
                        "'" + component.getMarkupId() + "'", "click", script.toString()));
            }
    
            @Override
            protected void updateAjaxAttributes(AjaxRequestAttributes attributes) {
                super.updateAjaxAttributes(attributes);
                attributes.setMethod(Method.POST);
            }
    
            @Override
            protected void respond(AjaxRequestTarget target) {
                StringValue parameterValue = RequestCycle.get().getRequest()
                        .getPostParameters().getParameterValue("dataForServer");
                onCallback(parameterValue.toString(), target);
            }
    
            protected void onCallback(String fromClient, AjaxRequestTarget target) {
                // overide to handle callback
            }
        }
    
    

    What wicket do render in the head-section:

    
        Wicket.Event.add('label1', "click", function(event) { 
            alert('Callback will follow!');
            var dataForServer='From client with love.';
            var attrs = {"u":"./?0-2.IBehaviorListener.0-label","c":"label1","m":"POST"};
            var params = {'dataForServer': dataForServer};
            attrs.ep = params;
            Wicket.Ajax.ajax(attrs);
        ;});