6

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.

mentxugle
  • 83
  • 4
  • 1
    If anyone is interested, found a solution for this type of scenario here: https://github.com/jadekler/git-web-requiredemo – mentxugle Feb 13 '14 at 18:33
  • So does it boil down to using `map` to redefine the `jquery` module based on the module depending on it? – anton.burger Feb 14 '14 at 12:35
  • 1
    To my understanding, map tells requireJS: ok, when this module "oldplugin" asks for "jquery", you're going to return this other js instead of the one defined for module "jquery". Be really careful though with the jQuery namespace when using something like require(["jquery","oldplugin"], function($){ (...)}) as that $ will represent the "default" jquery version and not the one loaded via map by require JS for the "oldplugin". – mentxugle Feb 17 '14 at 12:35
  • Hello, in Require js there is shim:{} configuration you can use that to resolve use – vaibhav.patil Mar 11 '15 at 13:40
  • Please add the answer you found so that this question will be moved off the Unanswered tab. – Vishwanath Mar 16 '15 at 13:14

1 Answers1

-2

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.