Pass Groovy list correctly in Javascript code in GSP

I am making a web application with Grails. I have a list with data that must be included in JavaScript in order to perform some dynamic loading in <select>. Basically, I get a two-level list from the server, then the first level is presented in the frame. When the user selects a parameter, the list associated with this parameter is displayed in another window.

The code (simplified) on the gsp page for the JavaScript function is as follows

function selecTipe() {
        var types = ${typeList}
        alert('List of types ' + types )

The problem is that if typeList is defined (in Groovy) as

typeList = [['TYPE1', ['VAR1','VAR2','VAR3']], 
            ['TYPE2', ['VAR1','VAR2','VAR3']]
            ['TYPE3', ['VAR1','VAR2','VAR3']] ]

when the page renders, the JavaScript code looks like

function selecTipe() {
        var types = [[ TYPE1, [ VAR1, VAR2, VAR3 ]], 
                     [ TYPE2, [ VAR1, VAR2, VAR3 ]]
                     [ TYPE3, [ VAR1, VAR2, VAR3 ]] ]
        alert('List of types ' + types )

which is erroneous, because JavaScript then treats not as strings, but as links due to the absence of quotes.

Groovy ?

PD: , , ...

EDIT: ,

+5
1

grails:

def types = ['TYPE1', 'TYPE2', 'TYPE3'] as grails.converters.JSON
[typeList : types]
+8

All Articles