Grails GSP accessing variables within script

六眼飞鱼酱① 提交于 2019-12-08 12:30:54

问题


I have searched for this but cannot seem to find the right solution. I am going to dumb down this a bit and see where I am going wrong.

I am using Grails for a project and I am really enjoying it. Where I think it is hard to get around is in the GSP's.

Can someone tell me given:

Controller:

def index()
{
   def message = "This is a test"
   [message: message]
}

Within the view

<!DOCTYPE html>
<html lang="en">
<head>
    <script type="text/javascript">
        $(document).ready(function() {

             HOW DO YOU ACCESS THAT VARIABLE IN HERE 

        });

<body></body>
</html>

I am trying to feed some 3rd party javascript libs and I am having an issue trying to figure this out. Yes, I can manually ajax it or use the 3rd party library to call an ajax thingy. However, I thought there was a grails way for this so that I do not have to do so much heavy lifting. I mean are you pros out there accessing it above the script {} and making all things global? Just trying to figure out what the correct way of doing this is.

Example library I am using is the JQwidgets library which does have ajax calls, but I would rather pass in arrays and such directly into via named actions in Grails.


回答1:


You can write like this:

<!DOCTYPE html>
<html lang="en">
<head>
    <script type="text/javascript">
        $(document).ready(function() {
            var message = "${message}";
            console.log("I'm from GSP", message);
            $("input[selector]").val(message);    // Just a dumb example
        });
    </script>
</head>
<body>
</body>
</html>



回答2:


Ok that as that led me down the path to the answer.

The Key was to put the codec stmt at the top of the gsp.

I can't help think that I'm hacking this together. Whenever I code by "google" I find out that Im not following what a "pro" would do. For now this seemed to solve this issue.

controller

def index() { 

    def configtree = ConfigTree.list()
    [message: configtree as JSON]
}


<%@page expressionCodec="none" %>
<!DOCTYPE html>

...

<script type="text/javascript">
    $(document).ready(function() {
    var data = ${raw(message)};

    // prepare the data
    var source =
    {
        datatype: "json",
        datafields: [
            { name: 'id' },
            { name: 'parentid' },
            { name: 'text' },
            { name: 'icon' },
            { name: 'expanded' },
            { name: 'selected' },
            { name: 'iconsize' },
            { name: 'disabled' },
            { name: 'value' }
        ],
        id: 'id',
        localdata: data
    };

    var dataAdapter = new $.jqx.dataAdapter(source);

    dataAdapter.dataBind();

    var records = dataAdapter.getRecordsHierarchy('id', 'parentid', 'items', [{ name: 'text', map: 'label'}]);


来源:https://stackoverflow.com/questions/33325965/grails-gsp-accessing-variables-within-script

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