Grails controller values (map) to separate javascript file

自作多情 提交于 2019-12-24 15:48:01

问题


I have checked a lot of forums, but I haven't got my answer. So - I have a lot of JS code, so i keep it in separate .js file (not in .gsp view). But now I have problems of accessing controller mapped values - I need them at my JS page, so I could generate my game field (based on values, that controller gives. I have

def createGame(long id) {
 //getting my game info object...
 [gameInfo:gameInfo]      
}

And then I accessed them in view, like - ${gameInfo.playerName} etc. But how to get them to JS file? For example, player has chosen an object's color (that is JS generated). One of examples could be ball.fillStyle(${gameInfo.ballColor}) But that, of course, is not working! Thanks in advance! :)


回答1:


You have to assign the values using inline JavaScript.

<script type="text/javascript">
 var gameInfo = ${gameInfo as JSON};
</script>


来源:https://stackoverflow.com/questions/17813367/grails-controller-values-map-to-separate-javascript-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!