通过scaladin覆盖原始的vaadin类方法包装器

时间:2012-12-11 10:48:19

标签: scala vaadin

我正在使用带有scaladin附加功能的vaadin&有一个类扩展vaadin.scala.CustomComponent

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

现在我希望在我的班级中使用com.vaadin.ui.Component类的覆盖附加方法在该方法中访问我的类属性(root,..)。

我该怎么做?

感谢。

1 个答案:

答案 0 :(得分: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
)
相关问题