RequireJS:处理与SHim和非AMD加载脚本的jQuery冲突

时间:2016-02-03 21:23:35

标签: javascript jquery requirejs amd

我有一个页面用RequireJS加载大多数模块,但也有一些第三方插件加载自己的JS代码(不使用RequireJS)。我页面的简单版本看起来像......

<script type="text/javascript">
    require.config({
        baseUrl: '/lib',
        shim: {
            "bootsShim": {
                deps: ["jquery"]
            },
        }
      });

        (function() {
           var thirdParty = document.createElement('script'); 
           thirdParty.type = 'text/javascript'; 
           thirdParty.async = true;
           thirdParty.src = "www.someserver.com/somecode.js";
           var s = document.getElementsByTagName('script')[0];     
           s.parentNode.insertBefore(thirdParty, s);
      })();
   </script>

这个somecode.js第三方库(注意它如何异步加载)与它自己的jQuery版本捆绑在一起。由于这也注册为命名的AMD模块,因此我的baseUrl中的jQuery存在冲突(也称为&#39; jquery&#39;)。因为我的jQuery和他们的版本不同。

由于我使用的是需要全局jQuery的shimmed库,我无法使用map {的this suggestion from the docs

如何最好地缓解这种冲突?

1 个答案:

答案 0 :(得分:0)

你不能保存你的jquery版本,同步加载它们的脚本,这样在它们加载后你可以在$ .ready中重命名保存的jquery吗?

<script src="jquery.js?yourversion"></script>
<script>myJquery = $;<.script>
<script src="www.someserver.com/somecode.js"></script>
<script>
$(document).ready(function(){
  $ = myJquery;
});
</script>
相关问题