1

我将 vaadin 与 scaladin 附加组件一起使用并有一个类扩展vaadin.scala.CustomComponent

class CostPlanImport extends CustomComponent {
    var root: Panel = _
    // ..
}

现在我想在我的类中覆盖com.vaadin.ui.Component类的附加方法并在该方法中访问我的类属性(根,..)。

我该怎么办?

谢谢。

4

1 回答 1

4

正如您从CustomComponent源代码中看到的那样

class CustomComponent(override val p: com.vaadin.ui.CustomComponent with
    CustomComponentMixin = new com.vaadin.ui.CustomComponent with 
    CustomComponentMixin
) extends AbstractComponent(p) {
    ... inner code
}

您将相应的 vaadin 组件作为构造函数参数p(通常代表“peer”)传递

也就是说,scaladin 组件是原始组件的包装器。

要覆盖对等组件的行为,您必须在将其传递给包装器构造函数时执行此操作。

你在这里有不同的选择


带有匿名子类

class CostPlanImport(override val p: com.vaadin.ui.CustomComponent with CustomComponentMixin =
    new com.vaadin.ui.CustomComponent {
        def attach() {
            //your code here
        } with CustomComponentMixin
) extends CustomComponent(p)

有特点

trait AttachBehavior {
    self: com.vaadin.ui.Component =>
      def attach() {
         //your reusable code here
      }
}

class CostPlanImport(override val p: com.vaadin.ui.CustomComponent with CustomComponentMixin =
    new com.vaadin.ui.CustomComponent 
        with CustomComponentMixin
        with AttachBehavior
) extends CustomComponent(p)

有一个CustomComponent子类

[未显示,而是前面两个示例的混合]


注意:代码示例未经测试


更新

如果您需要将“包装组件”构造函数中的参数传递给对等方的方法,您可以

class CostPlanImport(
    name: String,
    cost: Double,
) extends CustomComponent( p = 
    new com.vaadin.ui.CustomComponent {
        def attach() {
            //your code here
            //with "name" & "cost" visible
        } with CustomComponentMixin
)
于 2012-12-11T11:41:22.647 回答