2013-08-27 1 views
0

내 도메인 클래스에 설정된 모든 값을 클라이언트 측 (gsp 또는 자바 스크립트)으로 가져 오려고합니다. GeneralSetting이라는 도메인 클래스가 있다고 가정 해 봅니다. 내가 사용한 접근법은 내가 원하는 방식으로는 완벽하지는 않지만.자바 스크립트 (gsp)로 도메인 클래스 값 가져 오기

GSP 파일

<% 
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 : 빨간색

나는 이런 식으로 뭔가에 대해 생각했다 :

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

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

하지만 내가 배열을 OUPUT 때 난 그냥이 점점 계속 : [com.domain.GeneralSetting : 1] 대신 모든 속성의

답변

1

이 시도 ..

<%@ 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 
%> 

및 JS 부분을

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

그 다음

JSON.parse('${json}') 
JSON.parse('${json1}') 
+1

당신은 남자 야! 고맙습니다 – Jacob

관련 문제