Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using bindData method outside of controller

Tags:

grails

I was wondering if anyone had an idea for the best way to provide the functionality of bindData() outside of my grails controllers. In my current project I have created several groovy classes to model objects returned by an api. In these classes I have a static method that parses xml and returns a List of objects of the class. I would like to skip all the type casting nonsense by using the bindData method in these classes. Any suggestions on how to do this would be appreciated.

like image 386
NOLA Tech Avatar asked Dec 21 '09 23:12

NOLA Tech


1 Answers

I was looking for a similar solution, to use bindData in a service class. I found a solution in JT's blog. The solution is basically to import:

import org.codehaus.groovy.grails.web.metaclass.BindDynamicMethod

then add this to your code:

def foo = new Foo()
BindDynamicMethod bind = new BindDynamicMethod()
def args =  [ foo, params, [exclude:['name', 'mail']] ] // for example
bind.invoke( foo, 'bind', (Object[])args)

The (Object[]) cast is necessary du to Groovy/Java compatability. (Groovy is treating the ‘args’ object as an ArrayList, not an array of Objects.)

like image 200
Mr.B Avatar answered Nov 13 '22 06:11

Mr.B