我的 Grails 应用程序中有一个相当大的标准闭包,我想在应用程序的多个位置重用其中的一部分。我不想复制我需要重用的部分,而是想将其定义为一个单独的闭包,并在需要的地方引用它,但我在语法上有点困难。
这是一个简化/缩减的版本,但本质上我的标准看起来像这样:
def criteriaClosure = {
and {
// filtering criteria that I'd like to reuse in lots of places
or {
names.each { name ->
sqlRestriction(getFilteringSql(name), [someId])
}
}
if (isOrganisationChild(childDefaultGrailsDomainClass)) {
sqlRestriction(getFilteringSql(domain), [someArg])
}
// filtering criteria that's specific to this particular method
sqlRestriction(getSomeOtherSql(), [someOtherArg])
}
}
def criteria = domain.createCriteria()
def paginatedList = criteria.list([offset: offset, max: max], criteriaClosure)
我尝试定义我想要作为变量重用的闭包部分,并在我的标准闭包中引用它,但是它定义的限制似乎不适用。
def reusableClosure = {
and {
or {
names.each { name ->
sqlRestriction(getFilteringSql(name), [someId])
}
}
if (isOrganisationChild(childDefaultGrailsDomainClass)) {
sqlRestriction(getFilteringSql(domain), [someArg])
}
}
}
def criteriaClosure = {
and {
reusableClosure() //this doesn't seem to work
sqlRestriction(getSomeOtherSql(), [someOtherArg])
}
}
我确信这一定是一件非常简单的事情,所以如果这是一个愚蠢的问题,我深表歉意。有任何想法吗?