2012-12-09 52 views
0

我想使用Apache Wicket将AttributeAppender添加到AjaxEventBehavior内的组件。行为具有getComponent()方法,但在构造函数getComponent()obvioulsy中返回null。如何将行为添加到另一个组件中另一个行为添加到该组件的Wicket

现在我该组件传递给AjaxEventBehavior的构造,它的工作,但是这是一个很好的方式来实现我的目标..

下面是我在做什么:

AjaxTooltipBehavior:

public class AjaxTooltipBehavior extends AjaxEventBehavior { 
     public AjaxTooltipBehaviour(String event, Component tooltippedComponent) { 
      super(event); 
      tooltippedComponent.add(new AttributeAppender("data-tooltip","wicketAjaxTooltip")); 
     }  

     ... 
} 

这就是我的方式使用它:

... 
final WebMarkupContainer icon = new WebMarkupContainer("icon"); //a tooltiped icon 
icon2.add(new AjaxTooltipBehaviour("mouseover",icon2) 

我问自己是否没有办法将AttributeAppender添加到组件中,而不将组件传递到AjaxTooltipBehavior。 有谁知道这是可能的检票口还是有更好的解决方案? 仅供参考:我正在使用wicket 1.6。

在此先感谢您的支持! Ronny

回答

2

一般而言,您可以覆盖Behavior#onBind(Component),但此方法在AbstractAjaxBehavior中最终确定。但是,它会调用onBind()并使用getComponent()有:

@Override 
protected void onBind() { 
    super.onBind(); 
    getComponent().add(new AttributeAppender("data-tooltip","wicketAjaxTooltip")); 
} 
+0

此解决方案的工作原理,但我发现了一个更好的方法。我重写'onConfigure(Component component)',这似乎是与行为组件一起工作的正确位置。不过谢谢你! – rontron

0

因为您已经从AbstractAjaxBehavior(AjaxEventBehavior extends AbstractAjaxBehavior)扩展,所以您应该可以访问getComponent(),它将为您提供行为所附带的组件。

-1

我重写Behavior#onConfigure(Component component)至极可以与属于行为组件添加行为或做一些其他的东西最适合的方式。

@Override 
protected void onConfigure(Component component) { 
    super.onConfigure(); 
    component().add(new AttributeAppender("data-tooltip","wicketAjaxTooltip")); 
} 
+0

绝对不应该将attributeappender添加到onConfigure。这意味着,每次组件重新呈现时都会添加属性appender,例如,您将拥有data-tooltip =“wicketAjaxTooltip wicketAjaxTooltip”。 – Sarhanis

相关问题