Grails conflict between jQuery Plugin and JS script

夙愿已清 提交于 2020-01-16 20:05:31

问题


I have a Grails app that needs jQuery, so I load jQuery and jQuery-UI via plugins:

buildConfig.groovy:
    runtime ":jquery:1.11.1"
    compile ":jquery-ui:1.10.4"

That works fine as far as I don't include a standalone JS-script that also uses jQuery. This script is named myscript.jquery.js and I placed it under web-app/js.

Now I include those plugins and the script in my main.gsp:

main.gsp:
<html>
<head>
      ...

<r:require modules='jquery, jquery-ui, bootstrap, customcss' />
<g:javascript src="myscript.jquery.js"/>

<r:layoutResources disposition="defer" />

<g:layoutHead/>
<r:layoutResources disposition="head"/>

</head>
<body>
      ...

and I get

ReferenceError: jQuery is not defined
$ = jQuery;

Did I make a specific mistake? Is there a better way to handle this?

PS: I tried to load jQuery directly via script in main.gsp, but that didn't work out because I have other plugins that depend on the jQuery plugin :-/


回答1:


Add your script in ApplicationResources.groovy file located in config and then add that module in the page. This will solve the issue.

Example: In your conf/ApplicationResources.groovy

modules = {
    baseJS {
        resource url: 'js/myscript.jquery.js'
    }
}

In your gsp file

<r:require modules='jquery, jquery-ui, bootstrap, customcss, baseJS' />

This is the old blog but you will get the idea

http://www.anyware.co.uk/2005/2011/09/12/optimising-your-application-with-grails-resources-plugin/

Thanks



来源:https://stackoverflow.com/questions/28433860/grails-conflict-between-jquery-plugin-and-js-script

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