RequireJS - jQuery plugins with multiple jQuery ve

2019-02-13 19:33发布

问题:

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.