我正在尝试实现一个jQuery AJAX回调来接收第三方JavaScript库的事件。
它几乎可以工作! :)
jQuery AJAX帖子是服务器端的接收者。回调URL由AbstractDefaultAjaxBehavior生成,该行已添加到Panel中。在行为的响应方法中,我将Panel的子组件添加到AjaxRequestTarget。但这并没有重新绘制该组件。抛出没有异常,设置了markupId的输出。
有什么想法吗?我知道在哪里可以找到一个wicket组件在GitHub中做类似的事情等等。
答案 0 :(得分:0)
我的解决方案基于上述评论。要通过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); } }); } }
呈现回调的行为:
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 } }
头部显示了什么样的门票:
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); ;});