在Require.js中使用多个版本的jQuery [英] Using Multiple Versions of jQuery with Require.js

查看:173
本文介绍了在Require.js中使用多个版本的jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的情况是我必须在同一页面上运行两个版本的jQuery(基本上,有一个运行1.4.2的网站,我有一个运行需要1.8.2脚本的书签我知道这不是一个好主意,但我现在坚持不懈)。

I have a situation where I must have a two versions of jQuery running on the same page(Basically, there is a website running 1.4.2 and I have a bookmarklet that runs scripts that need 1.8.2 I know this is not a good idea but I'm stuck with it for now).

现有版本为1.4.2,所需的较新版本为1.8.2。

The existing version is 1.4.2 in one and the newer version needed is 1.8.2.

我正在使用require.js并看过帖子 这个问题 但不太明白什么是最好的方式:

I am using require.js and have seen a post in this question here but don't quite understand what'a the best way to go:

在我的情况下,我有一个模块main.js:

In my case I have a module main.js:

(function () {
var root = this;

require.config({
    baseUrl: "http://localhost:9185/scripts/app/"      
});

define3rdPartyModules();
loadPluginsAndBoot();

function define3rdPartyModules() {
    define('jquery', [], function () { return root.jQuery.noConflict(true); });              
    define('ko', [], function () { return root.ko; });       
}

function loadPluginsAndBoot() {      
    requirejs([], boot);
}

function boot() {        
    require(['bootStrapper'], function (bs) { bs.run(); });
}
})();

然后是其他一些类似于此的模块:

and then some other modules that look similar to this:

define('bootStrapper', ['jquery', 'presenter', 'repository', 'dataPrimer'],
function ($, presenter, repository, dataPrimer) {
    //some stuff here

我是missjs的新手在使用版本1.4.2加载main.js之前加载它,如下所示:

I'm am new to requirejs and am loading it before main.js is loaded using version 1.4.2 like this:

 $.getScript("http://localhost:9185/bundles/jsextlibs?v=GOyDBs-sBDxGR5AV4_K-m-   OK9DoE0LGrun5FMPyCO9M1", function () {     
    $.getScript("http://localhost:9185/Scripts/lib/require.js", function () {        
        $.getScript("http://localhost:9185/bundles/jsapplibs?v=9TJUN0_624zWYZKwRjap4691P170My5FUUmi8_fzagM1");
        $.getScript("http://localhost:9185/Scripts/main.js");
    });
});

有人可以请告诉我如何修改我的代码,以便我的所有模块都可以e版本1.8.2,不会干扰已在1.4.2上运行的代码。

Can someone please show me how to modify my code so that all my modules will be able to use version 1.8.2 without interfering with the code already running on 1.4.2.

谢谢

Davy

推荐答案

var reqOne = require.config({
context: "version1",
baseUrl: 'scripts/',
paths: {
    jquery: 'lib/jquery.min',
}
});

var reqTwo = require.config({
    context: "version2",
    baseUrl: 'scripts/',
    paths: {
        jquery: 'lib/jquery.modern',
    }
});

reqOne(["helper/util"], function(require,util) {
        //This function is called when scripts/helper/util.js is loaded.
            //If util.js calls define(), then this function is not fired until
                //util's dependencies have loaded, and the util argument will hold
                    //the module value for "helper/util".
//                    intheutil(); //This was done
                    console.log('util loaded');
});


reqOne(['require','jquery'],function(require,$){
    console.log( $().jquery );
});

reqOne(['require','jquery'],function(require,jq){
    console.log( jq().jquery );
});


reqOne(['require','jquery'],function(require,$){
    console.log( $().jquery);
});

reqTwo(['require','jquery'],function(require,$){
    console.log( $().jquery );
});

console.log('If no errors mean success!');

以上是我在main.js中使用的内容。有关完整实现的详细信息,请参阅我在github中的实现。 github.com

The above is what i used in main.js. For full implementation detials please see my implementation in github. github.com.

这里jquery.min是jquery版本1.x
和jquery.modern是jquery版本2.x

Here jquery.min is the jquery version 1.x and jquery.modern is the jquery version 2.x

我用过的console.log。因此,请在启用浏览器控制台的情以上答案基于Require.js的文档。所以我认为它必须是你案件的解决方案。

I have used console.log. So check the example with Browser console enabled. The above answer is based on the docs of Require.js. So i think it must be the solution for your case.

这是我的推荐。
Require.js

这篇关于在Require.js中使用多个版本的jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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