最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

grails - GSP tags in JavaScript - Stack Overflow

programmeradmin1浏览0评论

I had the following in the <head> of a GSP

<script type="text/javascript>
    $("button.remove-item").click(function() {
        $.ajax({
            url: "${createLink(action: 'remove', controller: 'cart')}",
            type: 'POST'
        });
    });
</script>

Notice that I'm using the Grails createLink tag to construct the URL that the AJAX request will post to. When I moved this code into checkout.js and replaced the block of code above with:

<script type="text/javascript" src="${resource(dir: 'js', file: 'checkout.js')}"></script>

the createLink tag is no longer evaluated by Grails. So it seems that Grails tags within <script> blocks are evaluated, but tags within .js files included by GSPs are not - is there a way to change this?

I had the following in the <head> of a GSP

<script type="text/javascript>
    $("button.remove-item").click(function() {
        $.ajax({
            url: "${createLink(action: 'remove', controller: 'cart')}",
            type: 'POST'
        });
    });
</script>

Notice that I'm using the Grails createLink tag to construct the URL that the AJAX request will post to. When I moved this code into checkout.js and replaced the block of code above with:

<script type="text/javascript" src="${resource(dir: 'js', file: 'checkout.js')}"></script>

the createLink tag is no longer evaluated by Grails. So it seems that Grails tags within <script> blocks are evaluated, but tags within .js files included by GSPs are not - is there a way to change this?

Share Improve this question asked Sep 7, 2011 at 15:17 DónalDónal 187k177 gold badges585 silver badges844 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 3

Check out the GSParse plugin to have css and js parsed as a gsp file:

http://nerderg./GSParse

http://grails/plugin/gsp-arse

You are right .js files are not evaluated by grails! but the GSP are! so thats why when u were setting a tag it was working. I would suggest you to have a differente approach of how to grab that link! as u are using jquery I would do like this:

<input type="button" class="remove-item" data-url="${createLink(action: 'remove', controller: 'cart')}" value="GO" />

checkout.js:

$("button.remove-item").click(function() {
    $.ajax({
        url: $(this).data('url'),
        type: 'POST'
    });
});
发布评论

评论列表(0)

  1. 暂无评论