confused on closures.

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

confused on closures.

Mauro
I have this method:

def filterCriteria() {
        
        def query = {
            
            filter.fieldCriteria.each { key, value ->
                if (!value.isEmpty()) {
                    switch(value) {
                        case FilterType.EQUALS.toString():
                            clazz.where { filter.fieldName[key] == filter.fieldValue[key] }
                            break

                            .................

in my controller I call
def qry = builder.filterCriteria(), 
If the swith case is ok I guess variable qry contains 
"clazz.where { filter.fieldName[key] == filter.fieldValue[key] }"
But if I do println qry is empty.
Reply | Threaded
Open this post in threaded view
|

Re: confused on closures.

longwa
The filterCriteria method is defining a Closure and, presumably, returning it. Calling

def qry = build.filterCriteria()

should result in qry being an instanceof Closure, right? The switch won't even execute until that closure is called. I presume clazz is a member variable in the builder class.

Lastly, I'm skeptical that you'll get the where() query to work with that syntax. The left side of the == may not work properly the way you have it. You may need to do new DetachedCriteria(clazz).build { eq filter.fieldName[key], filter.fieldValue[key] } instead.

-Aaron


On Wed, May 22, 2013 at 5:41 PM, Mauro <[hidden email]> wrote:
I have this method:

def filterCriteria() {
        
        def query = {
            
            filter.fieldCriteria.each { key, value ->
                if (!value.isEmpty()) {
                    switch(value) {
                        case FilterType.EQUALS.toString():
                            clazz.where { filter.fieldName[key] == filter.fieldValue[key] }
                            break

                            .................

in my controller I call
def qry = builder.filterCriteria(), 
If the swith case is ok I guess variable qry contains 
"clazz.where { filter.fieldName[key] == filter.fieldValue[key] }"
But if I do println qry is empty.

Reply | Threaded
Open this post in threaded view
|

Re: confused on closures.

Mauro



On 22 May 2013 23:51, Aaron Long <[hidden email]> wrote:

Lastly, I'm skeptical that you'll get the where() query to work with that syntax. The left side of the == may not work properly the way you have it. You may need to do new DetachedCriteria(clazz).build { eq filter.fieldName[key], filter.fieldValue[key] } instead.

What's the difference between  

new DetachedCriteria(clazz).build { eq filter.fieldName[key], filter.fieldValue[key] }

and 

clazz.createCriteria().list { eq filter.fieldName[key], filter.fieldValue[key] }