How To Pass A Value Directly From A Controller To A Javascript File In Grails
Would like to pass directly from the controller to a JavaScript file. TestControler.groovy def list() { render (view: 'list', model:[theId: theId]) } Test.js function test()
Solution 1:
Unfortunately there does not seem to be anything that covers what you're looking for. The best approach that I have seen, and have used myself, is to use hidden fields in the gsp/html, and retrieve those values from javascript on the client side.
Solution 2:
I have had similar issue this is how I solved it.
say you have this controller:
def list() {
render (view: "list", model:[theId: theId])
}
Then on your view( list.gsp)
<g:javascript>
var theId = ${theId}
</g:javascript>
Then your function on the external file can do anything with variable theId
because it global variable.
function test() {
// do smth with theId
}
Personally I was having a complex json object so instead of coding the json into html and prasing it back to json when needed on the client side, I just put it as js json variable from my gsp and then I can do whatever I want from the client side
Post a Comment for "How To Pass A Value Directly From A Controller To A Javascript File In Grails"