RequireJS - 在上下文中隔离jQuery版本和插件 [英] RequireJS - Isolating jQuery versions and plugins within contexts

查看:127
本文介绍了RequireJS - 在上下文中隔离jQuery版本和插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,它利用多个require上下文和动态加载的jQuery版本和插件。 (jQuery版本将集中采购,不会内联)。主机和每个上下文的jQuery完整性以及插件隔离都很关键。

I have a project which utilizes multiple require contexts and dynamically loaded jQuery versions and plugins. (The jQuery versions will be centrally sourced and will not be inlined). jQuery integrity for the host and each context as well as plugin isolation are critical.

我正在为RequireJS编写一个加载器插件来处理以下用例:

I am writing a loader plugin for RequireJS to handle the following use cases:


  • 在特定的需求上下文中

  • 在不影响托管页面的fn或任何其他需求上下文的情况下,我想:


    • 加载我需要的任何版本的jQuery

    • 将插件加载到我特定jQuery版本的fn上

    • 具有参照完整性需要jQuery版本,以便我的require上下文中的任何模块都可以共享插件

    • Within a specific require context
    • And without affecting the fn of the hosting page or any other require context, I would like to:
      • Load any version of jQuery I need
      • Load plugins onto the fn of my specific jQuery version
      • Have referential integrity on a required jQuery version such that any module within my require context can share plugins

      I有一个带有测试的加载器的示例项目这里

      I have an example project for the loader with tests here.

      当我使用加载程序进行第二次调用时,它在最后一个条件下失败,它正在吹走先前的版本。

      It is failing on the last condition when I make a second call using the loader, it is blowing away the prior version.

      我还使用eval将加载的插件中的$引用隔离到本地加载的jQuery版本范围。任何有关更好的方法的想法将不胜感激。

      I am also using eval to isolate the the $ references in the loaded plugins to the locally loaded jQuery version scope. Any thoughts on a better way to do this would be appreciated.

      如果有一个更容易/更清洁的解决方案,想要保持简单(愚蠢)。

      Want to Keep it Simple (Stupid) if there's an easier/cleaner solution.

      非常感谢。

      推荐答案

      我还必须在插件中执行此操作。您应该查看 http://api.jquery.com/jQuery.noConflict/ 。举个例子:

      I've also had to do this in a plugin. You should check out http://api.jquery.com/jQuery.noConflict/. To give an example:

      jQueryLocal = jQuery.noConflict(true);
      

      但你当然应该尽量避免加载多个jQuery。例如,您可以检查是否已经加载了足够的版本(在我的情况下,我在加载任何内容之前检查了1.9+是否可用,如果是这样,则将局部变量设置为对现有全局jQuery对象的简单引用,但是我没有使用插件。

      But you should certainly try to avoid loading multiple jQueries. You could, for example, check if a sufficient version is already loaded (in my case, I checked if 1.9+ was available before loading anything, and if so set the local variable to a simple reference to the existing global jQuery object, but then I wasn't using plugins.

      您可以通过以下方式查看版本:

      You can check the version with:

      var jQueryLocal;
      var versions = (jQuery && /^([0-9]+)\.([0-9]+)\./.exec( jQuery.fn.jquery )) || [0,0];
      if( versions[0] > 1 || versions[1] >= 9 ) {
          jQueryLocal = jQuery;
      } else {
          // load script with whatever you're doing. If it's asynchronous, set callbacks etc.
          jQueryLocal = jQuery.noConflict(true);
      }
      

      这篇关于RequireJS - 在上下文中隔离jQuery版本和插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆