jQuery 1.7 +,AMD(RequireJS)和Global Scope [英] jQuery 1.7+, AMD (RequireJS), and Global Scope

查看:97
本文介绍了jQuery 1.7 +,AMD(RequireJS)和Global Scope的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过AMD获得更多产品,并且在jQuery源代码中遇到了一些我不太了解的东西.

I'm trying to get more in line w/ AMD, and I've come across something in jQuery source that I just can't quite undertand.

以下是部分(位于文件末尾之前):

Here is section (found just before the end of the file):

if ( typeof define === "function" && define.amd && define.amd.jQuery ) {
    define( "jquery", [], function () { return jQuery; } );
}

据我所见,AMD的主要要点之一就是保持全球范围的清洁. jQuery选择返回对自身的引用作为模块,但仍会感染全局空间.

From what I can see, one of the major points of AMD is to keep the global scope clean. Yet jQuery chooses to return a reference to itself as a module, but still infects the global space.

在我看来,添加"jQuery.noConflict();"在返回之前就可以解决此问题,但仍将对象作为模块返回.

It seems to me, that adding "jQuery.noConflict();" just before the return would resolve this and still return the object as a module.

我知道RequireJS有一些专门用于jQuery的特殊文件,但是我不确定1.7+是否需要它.

I know that RequireJS has some special files specifically for jQuery, but I'm not sure that it's needed for 1.7+.

我的问题分为2部分:

1)有人知道为什么要做出这个决定吗?

1) Does anyone know why this decision was taken?

2)由于此方法不便于升级,是否有人熟悉使用RequireJS和jQuery标准版本的更优雅的解决方案?

2) Since this approach is not upgrade friendly, is anyone familiar with a more elegant solution that utilizes the standard version of RequireJS and jQuery?

推荐答案

好,刚发布后,我才意识到我可以通过另一个文件来代理它:

Ok, just after posting, I just realized I could proxy it through another file:

//main.js
require.config({
    paths : {
    jquery : 'my/libs/jquery-1.7.1.min',
    jQuery : 'my/src/jquery'
}

//my/src/jquery.js

define([
        'jquery'
    ],
    function($) {
        $.noConflict(true);

        return $;
    }
);

对主文件使用'jquery'别名而不是仅引用代理中的完全限定位置的原因是因为我正在使用AMD就绪的Backbone分支,该分支依赖于该别名:

The reason for the 'jquery' alias for the main file rather than just reference the fully qualified location in the proxy is because I'm using an AMD-ready branch of Backbone that depends on this alias:

https://github.com/jrburke/backbone/blob/2b0cfb4282f071cffb14a957d bone.js

作者已经接受了Document Cloud的一些提交,并希望也能吸收这种修改.

The author has had some commits accepted by Document Cloud and is hoping that this modification gets drawn in too.

看看这是否有缺陷,或者经过测试的AMD战役还有什么其他答案,将是很有趣的.

It will be interesting to see if there are any flaws with this or what additional answers there could be from the AMD battle tested.

这篇关于jQuery 1.7 +,AMD(RequireJS)和Global Scope的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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