RequireJS - jQuery plugins with multiple jQuery versions

99封情书 提交于 2019-12-19 03:22:31

问题


I am trying to wrap some legacy js code in modules so that they can be loaded with RequireJS.

I have three jQuery plugins, let's call them a,b and c.

  • Plugin a works with any version of jQuery
  • Plugin b works with jQuery version 1.4.2
  • Plugin c uses a and b, and works with any version of jQuery.

So anytime the plugin a is required, the jQuery that needs to be loaded is version 1.4.2. My require.config looks something like:

require.config({
   paths:{
        "jquery": "libs/jquery.1.9.1.min",
        "jquery1-4-2" : "libs/jquery1.4.2.min"
   });

And the plugins are defined as follows:

plugin a:
   define(["jquery"], function($){ (...) });
plugin b:
   define(["jquery1-4-2"], function($){ (...) });
plugin c:
   define(["jquery", "a","b"], function($){ (...) });

How can I configure this scenario so that if any plugin needs a certain version of jQuery, and the others don't require a version, that and only that certain version is loaded?

Thanks in advance.


回答1:


If you are really using jquery 1.4 this version doesn't support Requirejs, you should define de shim configuration, however you will have a conflict as jquery defines $ object globally, so it will overwrite any existing global $ variable. I think you can avoid this problem using jQuery.noConflict.



来源:https://stackoverflow.com/questions/21736531/requirejs-jquery-plugins-with-multiple-jquery-versions

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