我如何替换我自己的自定义动态脚手架方法 [英] How can I substitute my own custom dynamic scaffolding methods
问题描述
我的grails应用程序必须为每个域类的许多标准动态脚手架方法定义一些附加行为。
我知道我可以复制我需要为每个控制器添加的方法,事实上这正是我目前所做的。这确实意味着我的自定义代码被样板脚手架掩盖了。
我已经尝试导入和修改模板,但它们似乎只是在我生成静态在我的控制器脚手架。毋庸置疑,这并没有什么帮助。
拦截器似乎并不是我想要的,因为它们将行为包含在内而不是插入到行为中。我想过以某种方式拦截GORM调用,但那不是我真正想要的。
我真正想要做的是将基础动态脚手架方法替换为那些在我希望能够修改的地方拥有钩子的人。
以下显示了我尝试实现的示例
//标准保存动态脚手架方法用自定义代码的钩子装饰
pre
def save(){
def $ {propertyName} = $ {propertyName} .save(flush:true)){$ b $ new $ {className}(params)
saveBeforeSave($ {propertyName})
$ b render(view:create,model:[$ {propertyName}:$ {propertyName}])
return
}
saveAfterSave($ {propertyName})
flash.message = message(code:'default.created.message',args:[message(code:'$ {domainClass.propertyName} .label',default:'$ {className}') ,$ {prope rtyName} .id])
重定向(action:show,id:$ {propertyName} .id)
}
//占位符挂钩在控制器中被覆盖为必需
def saveBeforeSave($ {propertyName}){
}
def saveAfterSave($ {propertyName}){
}
解决方案不能简单地使用命令:
grails install-templates
只需配置您的控制器即可
$ p $static scaffold = * MODEL *
并将您的修改应用于 /src/templates/scaffolding/Controller.groovy
没有必要使用generate-controller生成
My grails app has to define some additional behaviour for many of the standard dynamic scaffolding methods for each domain class.
I know I can duplicate the methods I need to add to for each controller, and indeed that is what I currently do. This does mean that my custom code is obscured by the boilerplate scaffolding.
I have tried importing and modifying the templates as well but they only seem to get involved if I generate static scaffolding in my controllers. Needless to say this doesn't help much.
Interceptors don't seem to be what I want either as they enclose the action rather than being inserted into it. I thought about intercepting the GORM call in some fashion but that isn't really what I want either.
What I really want to do is replace the base dynamic scaffolding methods with ones that have a hook in the places I want to be able to modify.
The following shows an example of what I am trying to achieve
// standard "save" dynamic scaffold method decorated with hooks for custom code
def save() {
def ${propertyName} = new ${className}(params)
saveBeforeSave(${propertyName})
if (!${propertyName}.save(flush: true)) {
render(view: "create", model: [${propertyName}: ${propertyName}])
return
}
saveAfterSave(${propertyName})
flash.message = message(code: 'default.created.message', args: [message(code: '${domainClass.propertyName}.label', default: '${className}'), ${propertyName}.id])
redirect(action: "show", id: ${propertyName}.id)
}
// Placeholders hooks to be overridden in controller as necessary
def saveBeforeSave(${propertyName}) {
}
def saveAfterSave(${propertyName}) {
}
Can't you simple use the command:
grails install-templates
Then modify the controller ? Guide and Reference.
Just configure your controller with
static scaffold = *MODEL*
and apply your modifications to /src/templates/scaffolding/Controller.groovy
There is no need to generate to use generate-controller
这篇关于我如何替换我自己的自定义动态脚手架方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!