RequireJS configuration for jQuery

丶灬走出姿态 提交于 2020-01-15 11:22:55

问题


I'm trying to configure RequireJS to load jQuery from a different folder on my site than the /Scripts folder where everything else lives. I'm watching the page load with Fiddler, and I always see the jQuery request go out for /Scripts/jquery.js instead of the right URL.

In my markup, I have this:

<script src="@Url.Content("~/Scripts/require.js")" type="text/javascript" data-main="@Url.Content("~/Scripts/main")"></script>

In /Scripts/main.js I have this:

require.config({
    waitSeconds: 10,
    paths: {
        "jquery": "/N2/Resources/Js/jquery-1.7",
        "modernizr": "modernizr-2.0.6-development-only"
    }
});

require(["jquery", "jquery.utility", "jquery.link-decorators", "jquery.google-analytics", "accessibility", "modernizr"], function ($) {

... omitted

});

In Fiddler, I see the correct modernizr request go out, but the request for jQuery goes out for /Scripts/jquery.js.

As a test, I changed my modernizr line to "modernizr" : "/N2/Resources/Js/modernizr-2.0.6-development-only", and I see the request go out for that resource (of course it returns a 404). So I know that RequireJS sees my configuration, and is processing it.

NOTE: I'm aware of the various ways to deal with jQuery in RequireJS. All my plugins define modules, so out-of-order loading won't be a problem. I'm using jQuery 1.7, which is AMD-aware.


回答1:


I figured it out. In markup in the body of my page, I have

<script type="text/javascript">
require(["jquery", "Rotator"], function ($, Rotator) {
    $(document).ready(function () {
        var homeRotator = new Rotator();
        homeRotator.initialize($('#home-rotator ul.rotator-content li'), { viewTime: 30000, transitionTime: 2500 });
    });
});

This is what's causing the request for /Scripts/jquery. The Rotator module depends on jquery. If I remove the jquery dependency here, the page loads properly. The problem seems to be that I put my require.config settings into main.js, which is is loaded asynchronously by require.js. Before main.js is loaded, the script above is encountered in the body, so the request for jQuery goes out to the default location, not the configured location.

I moved my configuration settings into a <script/> tag in the page head, and now it's working.



来源:https://stackoverflow.com/questions/9830290/requirejs-configuration-for-jquery

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