繁体   English   中英

Grails:在GORM中设置域类属性时如何执行操作

[英]Grails: how to perform action when a Domain Class property is set in GORM

有没有办法让Domain Class setter有必要的操作。 这是我希望的工作,是否可能采取其他方式?

域类:

Class ExampleDomain {
  BigDecimal someNumber
  def setSomeNumber = {setVal ->
    println "Today is a good day to be the number: ${setVal}"
  }

}

我可以仅在onUpdate和类似事件上绑定事件吗,还是可以对Java对象驱动器事件进行更改?

例如:

def thisThing = new ExampleDomain(someNumber:3.0) //prints "Today is a good day to be the number: 3.0"
thisThing.someNumber = 5.8  //prints "Today is a good day to be the number: 5.8"
thisThing.save()  //prints nothing

那是可能的行为吗?

您可以通过定义getter / setter方法而不是闭包来完成此操作:

Class ExampleDomain {
  BigDecimal someNumber

  void setSomeNumber(someNumber) {
    println "Today is a good day to be the number: ${someNumber}"
    this.someNumber = someNumber
  }

  BigDecimal getSomeNumber() {
    someNumber
  }
}

效劳于

//prints "Today is a good day to be the number: 5.8"
new ExampleDomain().someNumber = 5.8  

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM