1

I need to manage a groovy list object in my javascript function. I have a groovy block into a javascript function. I tried with this:

var obj = ${mediaObjectInstanceList as grails.converters.JSON}[index];

and this:

var obj = ${mediaObjectInstanceList.get(index)};

but both are wrong. In the second I would specify the "index" int javascript variable out of the groovy block.

2 Answers 2

1

You can't do that in a way you defined it.

Though, there are lots of possibilities to accomplish that.

One of them is to define a JS-variable and fill it with groovy model values:

var myArray = new Array( 
<g:each in="${mediaObjectInstanceList}" var="obj" status="ix">
  ${status ? ',' : ''} '${obj}'
</g:each>
);

then you can traverse this array in JS using plain subscript notation:

var someVal = myArray[ index ];
Sign up to request clarification or add additional context in comments.

4 Comments

var myArray = new Array( ${mediaObjectInstanceList.collect { it as JSON}.join(',')} ); ?
this might not work, if one obj contains a single quote. My JS is very rusty, but would not ${new JSON(["q'q",'dq"dq']).toString() } suffice?
why would I need to convert the object into JSON to be converted to String again? In order to waste the resources only? To fight the single quote a simple replaceAll() would suffice
I couldn't solve my issue like that unfortunately. I needed to create a second object list excluding some of them and then passing it as a form input value. But unfortunately groovy works only with strings, it isn't possible to pass a list of object. I found another solution that I've explained as a comment in the under answer. Thanks for your contributions!
0

As answered by injecteer, you can't do that. Mostly because :

  • groovy blocks are executed server side : they can't be aware of variables in javascript only know by the browsers.
  • javascript is executed client side, ie. browsers don't know the variable mediaObjectInstanceList (only known by your grails application).

Two (main) solutions :

  • you don't know your index when the page is generated (no params in your request) => You have to generate the whole array server side (groovy) to be available at client side (javascript).

    var mediaObjectInstanceListInJS = new Array( ${mediaObjectInstanceList.collect { it as JSON}.join(',')} ); var someVal = mediaObjectInstanceListInJS[index];

  • you already have the index server side (with params in your request) => you can get in groovy block only your selected object :

    var someVal = ${mediaObjectInstanceListInJS[params.index] as JSON} ;

1 Comment

Thanks, I really didn't think about the two different elaboration times (server side for groovy and client side for javascript). Basically I wanted to create a object list to pass to the controller including only the checked object. I solved the problem naming the checkboxes with check_<objID> and then creating a list of checked ids using javascript and adding it as a hidden form input. So, the controller's method can reload the objects using the ids in the list. Thanks for have enlightened me :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.