简体   繁体   English

使用noConflict jQuery包含带有requireJS的jQuery UI

[英]Include jQuery UI with requireJS using noConflict jQuery

I'm working on a JavaScript module that uses jQuery, some functions of jQuery UI (draggable) and jPlayer. 我正在开发一个使用jQuery的JavaScript模块,jQuery UI(draggable)和jPlayer的一些功能。 Recently I made myself familiar with requireJS to manage the dependencies properly. 最近我让自己熟悉requireJS来正确管理依赖项。

I don't want to produce conflicts with a possibly already existing jQuery version that the site that includes my script uses. 我不想与包含我的脚本的网站可能已经存在的jQuery版本产生冲突。 For this reason I am mapping the jQuery dependencies to a module "jquery-private" with a call of noConflict(), as is described in the requireJS guide . 出于这个原因,我将jQuery依赖项映射到模块“jquery-private”,调用noConflict(),如requireJS 指南中所述

As jQuery UI takes up a lot of space, I would also like to just include the modules that I am actually using. 由于jQuery UI占用了大量空间,我还想包含我实际使用的模块。 ui.draggable has the dependencies ui.core, ui.mouse and ui.widget, so I should have to include these 4 modules. ui.draggable有依赖项ui.core,ui.mouse和ui.widget,所以我应该包含这4个模块。

My problem is that I would like the jQuery UI modules and the jPlayer module to use my own version of jQuery, but obviously it isn't accessible by the global $ variable after I called the noConflict() method. 我的问题是我希望jQuery UI模块和jPlayer模块使用我自己的jQuery版本,但显然在调用noConflict()方法后,全局$变量无法访问它。 Unfortunately neither jQuery UI nor jPlayer are AMD modules, so I needed to make shim configurations for them. 不幸的是,jQuery UI和jPlayer都不是AMD模块,因此我需要为它们制作垫片配置。

Here is my definition of the dependencies: 这是我对依赖项的定义:

require.config({
    baseUrl: 'javascript/modules',
    paths: {
        jquery: 'jquery-2.1.3',
        jPlayer: 'jquery.jplayer',
        uiCore: 'jquery.ui.core',
        uiMouse: 'jquery.ui.mouse',
        uiWidget: 'jquery.ui.widget',
        uiDraggable: 'jquery.ui.draggable'
    },
    map: {
      // '*' means all modules will get 'jquery-private'
      // for their 'jquery' dependency.
      '*': { 'jquery': 'jquery-private' },

      // 'jquery-private' wants the real jQuery module
      // though. If this line was not here, there would
      // be an unresolvable cyclic dependency.
      'jquery-private': { 'jquery': 'jquery' }
    },
    shim: {
        jPlayer: {
            deps: ['jquery']
        },
        uiCore: {
            deps: ['jquery']
        },
        uiMouse: {
            deps: ['jquery','uiCore']
        },
        uiWidget: {
            deps: ['jquery','uiCore']
        },
        uiDraggable: {
            deps: ['jquery','uiCore','uiMouse','uiWidget']
        }
    }
});

require(["json","jquery","jPlayer","uiDraggable"], function(json,___jQuery,jplayer,uiDraggable) {
    (...)
}

Obviously this code produces errors as the $ variable in the jQuery UI modules is not defined. 显然,由于未定义jQuery UI模块中的$变量,此代码会产生错误。

Is there any way to pass my own jQuery object to modules? 有没有办法将我自己的jQuery对象传递给模块? The top answer in another thread ( How use require.js to load jQuery with noConflict ) suggests that what I am trying to do is not possible, but maybe there is some other way to do it? 另一个线程中的最佳答案( 如何使用require.js加载带有noConflict的jQuery )表明我想要做的事情是不可能的,但也许有其他方法可以做到这一点?

If there is none, I probably have to use global variables and heavily edit the included modules, which kind of makes it questionnable why to use a dependency management library like requireJS in the first place... 如果没有,我可能不得不使用全局变量并大量编辑包含的模块,这使得为什么首先使用像requireJS这样的依赖管理库有疑问...

I found the following code on top of each module in jquery.ui: 我在jquery.ui中的每个模块的顶部找到了以下代码:

(function( factory ) {
    if ( typeof define === "function" && define.amd ) {
        // AMD. Register as an anonymous module.
        define([ "jquery" ], factory );
    } else {
        // Browser globals
        factory( jQuery );
    }
}(function( $ ) {...});

And it means jquery.ui checks when global AMD "define" function is defined and uses 'jquery' as AMD reference for module. 这意味着jquery.ui会检查何时定义全局AMD“定义”函数,并使用“jquery”作为模块的AMD参考。

It will use no conflict of jquery based on requirejs recommendation in this and this . 它将利用基于在requirejs建议没有jQuery的冲突这个这个

And about how to use jQuery with AMD. 以及如何在 AMD中使用 jQuery。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM