2017-05-31 49 views
0

我正在使用AjaxLink,并且我想在某些情况下停止它的“onClick”方法执行。对此的解决方案是名为“AjaxCallListener”的类,它具有方法“getPrecondition()”,您可以在其中调用任何您的javascript函数,并且结果为“false”,然后AjaxLink的“onClick”不会执行。这个“AjaxCallListener”被添加到ajaxRequest的属性中。Wicket AjaxCallListener getPrecondition方法不起作用

@Override protected void updateAjaxAttributes(AjaxRequestAttributes attributes) { 
    attributes.getAjaxCallListeners().add(new AjaxCallListener() { 
     @Override public CharSequence getPrecondition(Component component) { 
      return "isPropagationAllowed();"; 
     } 
    }); 
} 

那么在我们的js文件的地方:

function isPropagationAllowed() { alert('function called') return false; }

警报被调用,但在服务器上的进一步执行,仍然有效。

+0

如果youremove警报( '')?还是不行? –

+1

它没有工作,因为该函数的结果没有返回,所以它需要在Java中明确地完成 –

回答

3

你应该写 “返回$ {functionName}()” 作为 “getPrecondition” 方法的结果:

@Override protected void updateAjaxAttributes(AjaxRequestAttributes attributes) { 
attributes.getAjaxCallListeners().add(new AjaxCallListener() { 
     @Override public CharSequence getPrecondition(Component component) { 
      return "return isPropagationAllowed()"; 
     } 
    }); 
}