views:

196

answers:

1

Hi,

I have a feeling that the answer to this is easy but I can't seem to get it yet...

I have a Javascript function that calls an action in a Groovy class via a remoteFunction. Now, I need to return the value from the action class back to the Javascript function for further processing. It looks something like this:

// sample js function in the gsp file
function setPrev() {
   ...
   var tmp = $remoteFunction(action: 'someaction', params: ...)
   ...
}

In the controller, the action would look something like this:

def someaction = {
   ...
   def retVal = Somedomain.anobject // 1:m relationship so there's a hasMany = [anobject...] in Somedomain
   ...
   return retVal
}

Now, when it returns to tmp, it's [object object]. Is there a way for me to get, at the very least, [anobject1, anobject2, ..., anobjectn] as the value of tmp? I've even tried json but it's still [object object].

A: 

it is quite easy

 return retVal as JSON

Just remember to import org.grails.converters.JSON (I think it is)

sbglasius
hi, ya, i did this but when i printed out tmp, it's [object object]... not the json representation...
callie16
hi, that's more a javascript issue.. if you use FireBug in FireFox (and you should!), try to write console.debug(tmp) in your javascript code. To get your data, you should write tmp.variableName.
sbglasius
Thanks for the response :)Actually, I've been looking at the DOM via Firebug but i'm a little lost there as well. Anyway, as for the tmp.variableName, that's a little tricky, I think... I can't seem to figure out what the variableName is. retVal is, when I print out the class type in groovy, of type PersistentSet... in this case, it should look like [object1: 1, object2: 2, ...] So should I reference it as tmp.object1? When I did, it told me that it was "undefined" but when I look at retVal, it does have [object1: 1, object2: 2, ...] as the content...
callie16
in firebug you could also try console.dir(tmp) - this will print the structure and variables of your json
sbglasius