Wicket AjaxLink isLinkEnabled() = false 呈现可点击范围
Wicket AjaxLink isLinkEnabled() = false renders a clickable span
我正在使用 Wicket 6.11 并且遇到了一个奇怪的错误。我在一个大型应用程序中有带有图标的 wicket AjaxLinks,根据各种情况,其 isLinkEnabled() 可以 return false。当它这样做时,它会像您预期的那样将 link 呈现为禁用状态(其中带有 em 标记的 span 标记),但是当我单击该图标时,事件仍然会触发!
示例代码:
AjaxLink<Object> button = new AjaxLink<Object>( "editLocationButton" ) {
private static final long serialVersionUID = 1L;
public void onClick( AjaxRequestTarget p_target ) {
// do things
}
/**
* @see org.apache.wicket.markup.html.link.AbstractLink#isLinkEnabled()
*/
@Override
protected boolean isLinkEnabled() {
return super.isLinkEnabled() && getSelectedLocation() != null;
}
};
Html 文件:
<td style="width:0%">
<a href="#" wicket:id="editLocationButton" class="editButton iconButton">
<wicket:message key="button.edit.location"></wicket:message>
</a>
</td>
禁用时呈现 html:
<td style="width:0%">
<span class="editButton iconButton" id="editLocationButton7b6"><em>
</em></span>
</td>
editButton/iconButton CSS 设置背景图像和 width/height。
在 Firefox 元素检查器(不是 firebug)中查看,span 有一些事件附加到它触发对服务器的 ajax 调用,那么为什么它们没有被禁用?这是 Wicket 中的错误吗?我只是偶然发现它,因为我试图点击一个在演示期间看起来被禁用的 link!
感谢任何帮助,谢谢。
好吧,这似乎是 AjaxLink 实现中的一个错误。
它将添加具有以下实现的 AjaxEventBehavior:
protected AjaxEventBehavior newAjaxEventBehavior(String event)
{
return new AjaxEventBehavior(event)
{
private static final long serialVersionUID = 1L;
@Override
protected void onEvent(AjaxRequestTarget target)
{
onClick(target);
}
@Override
protected void updateAjaxAttributes(AjaxRequestAttributes attributes)
{
super.updateAjaxAttributes(attributes);
AjaxLink.this.updateAjaxAttributes(attributes);
}
};
}
如您所见,此实现并不关心 isLinkEnabled()
方法已被覆盖。因此,唯一的解决方法是切换到 isEnabled()
这将隐式停用该组件的所有子组件。 (根据您的 getSelectedLocation()
方法的性质,我建议在组件的 onConfigure()
部分执行此操作)
要禁用 link,您应该像这样在 link 上设置 setEnabled(false)
:
AjaxLink al = new AjaxLink("link") {
@Override
public void onClick(AjaxRequestTarget target) {
//do something on click
}
@Override
protected void onConfigure() {
setEnabled(someCondition());
}
};
方法 boolean isLinkEnabled()
只是一个帮手,因为它 JavaDoc 说:
Helper methods that both checks whether the link is enabled and
whether the action ENABLE is allowed.
我正在使用 Wicket 6.11 并且遇到了一个奇怪的错误。我在一个大型应用程序中有带有图标的 wicket AjaxLinks,根据各种情况,其 isLinkEnabled() 可以 return false。当它这样做时,它会像您预期的那样将 link 呈现为禁用状态(其中带有 em 标记的 span 标记),但是当我单击该图标时,事件仍然会触发!
示例代码:
AjaxLink<Object> button = new AjaxLink<Object>( "editLocationButton" ) {
private static final long serialVersionUID = 1L;
public void onClick( AjaxRequestTarget p_target ) {
// do things
}
/**
* @see org.apache.wicket.markup.html.link.AbstractLink#isLinkEnabled()
*/
@Override
protected boolean isLinkEnabled() {
return super.isLinkEnabled() && getSelectedLocation() != null;
}
};
Html 文件:
<td style="width:0%">
<a href="#" wicket:id="editLocationButton" class="editButton iconButton">
<wicket:message key="button.edit.location"></wicket:message>
</a>
</td>
禁用时呈现 html:
<td style="width:0%">
<span class="editButton iconButton" id="editLocationButton7b6"><em>
</em></span>
</td>
editButton/iconButton CSS 设置背景图像和 width/height。
在 Firefox 元素检查器(不是 firebug)中查看,span 有一些事件附加到它触发对服务器的 ajax 调用,那么为什么它们没有被禁用?这是 Wicket 中的错误吗?我只是偶然发现它,因为我试图点击一个在演示期间看起来被禁用的 link!
感谢任何帮助,谢谢。
好吧,这似乎是 AjaxLink 实现中的一个错误。 它将添加具有以下实现的 AjaxEventBehavior:
protected AjaxEventBehavior newAjaxEventBehavior(String event)
{
return new AjaxEventBehavior(event)
{
private static final long serialVersionUID = 1L;
@Override
protected void onEvent(AjaxRequestTarget target)
{
onClick(target);
}
@Override
protected void updateAjaxAttributes(AjaxRequestAttributes attributes)
{
super.updateAjaxAttributes(attributes);
AjaxLink.this.updateAjaxAttributes(attributes);
}
};
}
如您所见,此实现并不关心 isLinkEnabled()
方法已被覆盖。因此,唯一的解决方法是切换到 isEnabled()
这将隐式停用该组件的所有子组件。 (根据您的 getSelectedLocation()
方法的性质,我建议在组件的 onConfigure()
部分执行此操作)
要禁用 link,您应该像这样在 link 上设置 setEnabled(false)
:
AjaxLink al = new AjaxLink("link") {
@Override
public void onClick(AjaxRequestTarget target) {
//do something on click
}
@Override
protected void onConfigure() {
setEnabled(someCondition());
}
};
方法 boolean isLinkEnabled()
只是一个帮手,因为它 JavaDoc 说:
Helper methods that both checks whether the link is enabled and whether the action ENABLE is allowed.