RequireJS-具有多个jQuery版本的jQuery插件 [英] RequireJS - jQuery plugins with multiple jQuery versions
问题描述
我正在尝试将一些旧式js代码包装在模块中,以便可以将它们与RequireJS一起加载.
I am trying to wrap some legacy js code in modules so that they can be loaded with RequireJS.
我有三个jQuery插件,我们将它们分别称为a,b和c.
I have three jQuery plugins, let's call them a,b and c.
- 将作品与任何版本的jQuery兼容
- 插件b适用于jQuery 1.4.2版
- 插件c使用a和b,并且可以与任何版本的jQuery一起使用.
因此,每当需要插件a时,需要加载的jQuery版本是1.4.2. 我的require.config看起来像:
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"
});
插件定义如下:
plugin a:
define(["jquery"], function($){ (...) });
plugin b:
define(["jquery1-4-2"], function($){ (...) });
plugin c:
define(["jquery", "a","b"], function($){ (...) });
如何配置此方案,以便如果任何插件需要某个版本的jQuery,而其他插件不需要某个版本,则仅加载该特定版本?
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?
谢谢.
推荐答案
如果您确实在使用jquery 1.4,则此版本不支持Requirejs,则应定义de shim配置,但是在jquery定义$ object时会出现冲突.全局,因此它将覆盖任何现有的全局$变量. 我认为您可以使用jQuery.noConflict避免此问题.
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.
这篇关于RequireJS-具有多个jQuery版本的jQuery插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!