Question

I want to retrieve ALL values set in my domain class to the client side (gsp or Javascript) Let's say I have a domain class named GeneralSetting. The approach I used works but not completely how I want.

gsp file

<%
def test = com.domain.GeneralSetting.findAll()[0]
def test1 = com.domain.GeneralSetting.findAll()[0].color
%>

Js

console.debug('${test}'); OUTPUT: [com.domain.GeneralSetting : 1] console.debug('${test1}'); OUTPUT: red

I was thinking about something like this:

def globalSettings = com.digithurst.gutmann.domain.GeneralSetting.getAll()[0]
def array = []

//Add all properties
 globalSettings.each {
        array.add(it);
    }

But when I ouput the array i just keep getting this: [com.domain.GeneralSetting : 1] instead of all the properties

Was it helpful?

Solution

try this..

<%@ page import="grails.converters.JSON" %>

<%
def test = com.domain.GeneralSetting.findAll()[0] 
def json  = test as JSON
def test1 = com.domain.GeneralSetting.findAll()[0].color
def json1  = test1 as JSON
%>

and js part like

console.debug('${json}'); 
console.debug('${json1}');

then

JSON.parse('${json}')
JSON.parse('${json1}')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top