1

如果我有一个包含在 div 中的输入(连同其他东西),
我如何将任何添加到包装器的行为传递给该输入?

示例:
组件 HTML 就像<div> <img> <input> ...
然后我想添加它add( new MyComponent("foo", model).add( new AjaxEventBehavior("onupdate"){ ... }(但这不起作用,因为只能FormComponent接收(AjaxEventBehavior`s)。

我假设我可以将此行为对象从包装器移动到输入,但不知道在哪里 - 是否有一些“构建后”侦听器?
还是我应该完全不同,比如将行为作为参数传递?
保持它像上面那样的原因是为了使它对组件的用户透明。

4

2 回答 2

1

您可以覆盖MarkupContainer#add(Behavior... behaviors)包装组件的方法。这当然意味着包装组件永远不会收到任何行为:

public class MyWrappingComponent extends Panel {
    private TextField<?> field;

    // Constructors and stuff..

    @Override
    public Component add(Behavior... behaviors) {
        field.add(behaviors);
        return this;
    }
}

一种选择是将包装组件分离为 a Border,它可用于用内容包围组件。然而,这会降低包装元件的“透明度”。(参见Javadoc 页面上的示例)。

public class MyWrappingBorder extends Border {
    public MyWrappingBorder(String id) {
        // add the <img />
        // add the <div />
    }
    ...
 }

 // The markup
 <wicket:border>
     <div>
        <img />
        <wicket:body /> <!-- Will be replaced with the added content -->
     </div>
 </wicket:border>

然后像这样使用它:

 add(new MyWrappingBorder("border")
     .add(new TextField<String>("input").add(/* the behaviors */)));

有了这种标记

 <div wicket:id="border">
     <input type="text" wicket:id="input" />
 </div>

Border方法将使您能够重用没有“TextField”的边框组件?零件。例如,如果你想添加一个下拉菜单:

 add(new MyWrappingBorder("border")
     .add(new DropDownChoice<String>("input", listOfChoices)));    
于 2013-01-14T06:41:56.547 回答
1

在返回组件的包装器中包含一个抽象方法。

public abstract class WrapperComponent extends Panel 
{
       public WrapperComponent(String id)
       {
              super(id);

              add(getInnerComponent("whateveridyouwant"));

              //Other wrapper stuff
       } 

      public abstract Component getInnerComponent(String id);         
}

然后调用 WrapperComponent 将强制您重写 getInnerComponent 方法。

WrapperComponent wrapperComponent = new WrapperComponent("wrapperComponent")
{
     @Override
     public Component getInnerComponent(String id)
     {
          TextField textfield = new TextField(id);
          textField.add(behavior);
          return textfield; 
     }  

} 

这种方式允许您在实际决定使用 WrapperComponent 时定义您想要的内部组件和行为。它可以更好地控制您可以使用 innerComponent 做什么。

希望你觉得这很有帮助。

于 2013-01-14T09:08:03.780 回答