Where should I put javascript libraries in a Grails app?

久未见 提交于 2019-11-30 17:17:45

With your JS file at: web-app/js/myLib.js, putting <g:javascript library="myLib" /> in your layout should I think be what you need.

You should probably NOT use <g:javascript library="myLib" /> as that is meant to be used as a way to make AJAX calls library (scriptaculous, dojo, yahoo, jquery) indifferent. See grails javascript. Instead use <g:javascript src="myLib.js" />.

There are two ways by which you can include JS file and JS library like scriptaculous

// actually imports '/app/js/myscript.js'
<g:javascript src="myscript.js" />

// imports all the necessary js for the scriptaculous library
<g:javascript library="scriptaculous" />

<g:javascript>alert('hello')</g:javascript>

Refer : http://grails.org/doc/latest/ref/Tags/javascript.html

I think I found the answer...

If I use this tag in my gsp (rather than a straightforward javascript reference)

<g:javascript library="mylib" />     

Then when I look at the generated page source it refers to

<script type="text/javascript" src="/myapp/js/mylib.js"></script>

This corresponds to the folder web-app/js, so I dropped my script library in there and it works fine. The breakthrough was for me to a) rename my index.html to index.gsp and b) use the g:javascript tag.

Use <g:javascript src="myscript.js" /> and place your script in web-app/js/myscript.js

For full detail and examples for both your own external scripts, in-line code and libraries see http://grails.org/doc/latest/ref/Tags/javascript.html.

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