How do you share common methods in different grails controllers? -
currently when need share method processparams(params)
between different controllers, use either inheritance or services. both solution has inconvenients :
- with inheritance, cannot use multiple inheritance means need have of controller utility methods in 1 place. , also, there bug in grails not detect code changes in base controller classes in development mode (you need restart app)
- with services, don't have access injected properties params, session, flush...
so question : there other way use common methods accessible multiple controllers ?
one option write common methods category, mix controllers necessary. gives lot more flexibility inheritance, has access stuff params, , code simple , understandable.
here's tiny example:
@category(object) class mycontrollercategory { def printparams() { println params } } @mixin(mycontrollercategory) class somethingcontroller { def create = { printparams() ... } def save = { printparams() } }
Comments
Post a Comment