NOLA Tech
NOLA Tech

Reputation: 71

Using bindData method outside of controller

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.

Upvotes: 7

Views: 2192

Answers (1)

Mr.B
Mr.B

Reputation: 907

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.)

Upvotes: 9

Related Questions