简体   繁体   English

使用ReqiureJS拥有第三方库的本地实例

[英]Using ReqiureJS to have local instances of 3rd party libraries

My application's JavaScript uses jQuery and jQuery plugins and running in "hostile" environment I have no control over (this is PHP extension for eCommerce platform). 我的应用程序的JavaScript使用jQuery和jQuery插件,并且在我无法控制的“敌对”环境中运行(这是eCommerce平台的PHP扩展)。 Thus no way to determine whether my jQuery code will be executed before someone will attach his instance of jQuery/plugins (introduced by other extension) or after this or someone will load jQuery dynamically after page rendered. 因此,在有人将附加他的jQuery / plugins实例(由其他扩展名引入)之前或之后,或者在此之后有人将在页面呈现后动态加载jQuery时,无法确定是否执行我的jQuery代码。

Basically the problem is that other extension could use jQuery (with plugins) also and just connecting jQuery with tag will not work. 基本上,问题在于其他扩展也可以使用jQuery(带有插件),并且仅将jQuery与tag连接是行不通的。

I have a strong feeling that RequireJS might help me to load needed version of jQuery as far as particular versions of jQuery plugins into the encapsulated scope without polluting global scope (so other extensions will still function properly). 我有一种强烈的感觉,RequireJS可能会帮助我将所需版本的jQuery插件加载到封装的作用域中,直到特定版本的jQuery插件为止,而不会污染全局作用域(因此其他扩展仍将正常运行)。 Then I'll wrap all my code to "require" statements and it will run using it's own set of jQuery and plugins. 然后,将所有代码包装为“ require”语句,它将使用其自己的jQuery和插件集运行。 I tried this and it kind of works (have not tested this in production environment though) but in some weird way. 我尝试了这种方法,并且进行了某种工作(虽然还没有在生产环境中进行过测试),但是却有些奇怪。 Seems like this question is kind of relevant to problems I have. 似乎这个问题与我遇到的问题有关。 Also answer suggesting to use AMD-compatible version of jQuery. 还回答建议使用AMD兼容版本的jQuery。 But what about plugins? 但是插件呢? I don't think all plugins I use have such versions. 我不认为我使用的所有插件都具有这样的版本。

So questions: 所以问题:

Could RequireJS be used to cover such use case (running jQuery+plugins in undefined environment)? 可以使用RequireJS来涵盖这种用例(在未定义的环境中运行jQuery +插件)吗? If RequireJS could be used there then any example code or explanation of how to do this properly will be greatly appreciated. 如果可以使用RequireJS,那么将非常感谢任何示例代码或有关如何正确执行此操作的说明。 If there is no way to cover this with RequireJS what do you think would be best approach to handle issue? 如果没有办法用RequireJS涵盖这一点,那么您认为什么是处理问题的最佳方法?

Yes, I believe RequireJS can help you out here. 是的,我相信RequireJS可以在这里为您提供帮助。 To my knowledge you'll have to do some legwork, though. 据我所知,您将不得不做一些腿部工作。 Take a look at the source of the (as of 2012-08-19) latest jQuery: link . 看看(截至2012-08-19)最新jQuery: link的源代码。 At the bottom you can see that window.jQuery is being set, defining jQuery and $ . 在底部,您可以看到正在设置window.jQuery ,定义了jQuery$ The define call for AMD loaders happens after that, so jQuery is in the global scope no matter what. AMD加载程序的define调用在此之后发生,因此无论如何, jQuery都在全球范围内。 What you want to do is guarantee that your own version of jQuery and plugins stay isolated from other jQuery instances and plugins, correct? 您想要做的是确保您自己的jQuery和插件版本与其他jQuery实例和插件保持隔离,对吗?

Place your custom jQuery and plugins into their own directory (let's call it "jqcustom") and use that directory when specifying dependencies in your scripts' define calls. 将您的自定义jQuery和插件放在它们自己的目录中(我们将其称为“ jqcustom”),并在脚本的define调用中指定依赖项时使用该目录。 You'll want to modify your version of jQuery by wrapping it in a define call and returning the jQuery object at the very bottom, taking out jQuery's window.jQuery and window.$ assignments as well as its default define call in the process (these are both at the bottom, remember). 您需要修改jQuery版本,方法是将其包装在define调用中,并在最底部返回jQuery对象,取出jQuery的window.jQuerywindow.$赋值以及该过程中的默认define调用(这些都在底部,请记住)。 Then you'll need to wrap all of your plugins in define calls and specify your own version of jQuery as the dependency. 然后,您需要将所有插件包装在define调用中,并指定自己的jQuery版本作为依赖项。 Example: 例:

define(['jqcustom/jquery'], function(jQuery) {
    //inside this method 'jQuery' will override any predefined global window.jQuery

    //in case any plugins use the $ variable, define that locally too
    var $ = jQuery;

    //... plugin code ...

});

If you use RequireJS' optimizer, it can do the wrapping of the plugins for you with its shim config options. 如果您使用RequireJS的优化程序,它可以使用其shim config选项为您包装插件。 jQuery plugins work by adding methods to jQuery's prototype object, so I believe as long as you pass the same jQuery (your custom jqcustom/jquery one) to every plugin with your define wrapping, the plugins' extensions will all be set on the same object and be accessible in subsequent define calls specifying your custom jQuery or custom plugins as dependencies. jQuery插件通过向jQuery的原型对象添加方法来工作,因此,我相信,只要您使用define包装将相同的jQuery (您的自定义jqcustom/jquery一个)传递给每个插件,这些插件的扩展名都将设置在同一对象上并且可以在随后的define调用中访问,这些调用将您的自定义jQuery或自定义插件指定为依赖项。 Good luck! 祝好运!

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

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