问题
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