jQuery插件和JS脚本之间的Grails冲突 [英] Grails conflict between jQuery Plugin and JS script

查看:73
本文介绍了jQuery插件和JS脚本之间的Grails冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要jQuery的Grails应用,所以我通过插件加载jQuery和jQuery-UI:

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"

就我不包括也使用jQuery的独立JS脚本而言,这很好用.该脚本名为myscript.jquery.js,我将其放在web-app/js下.

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.

现在,我将这些插件和脚本包含在main.gsp中:

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>
      ...

我得到

ReferenceError: jQuery is not defined
$ = jQuery;

我犯了一个具体错误吗? 有更好的方法来解决这个问题吗?

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

PS:我试图直接通过main.gsp中的脚本加载jQuery,但是由于我还有其他依赖于jQuery插件的插件而无法解决:-/

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 :-/

推荐答案

将脚本添加到config中的ApplicationResources.groovy文件中,然后将该模块添加到页面中.这样可以解决问题.

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

示例: 在您的conf/ApplicationResources.groovy

Example: In your conf/ApplicationResources.groovy

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

在您的gsp文件中

<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/

谢谢

这篇关于jQuery插件和JS脚本之间的Grails冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆