Ian*_*Ian 2 javascript optimization jquery requirejs
我正在使用requireJS和一个CDN版本的jQuery(现在是推荐的方法)组建一个框架,并在优化代码时遇到一些问题.输出是命名空间的,我指定每个模块使用文档中概述的jquery的私有版本:
require.config({
// Add this map config in addition to any baseUrl or
// paths config you may already have in the project.
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' }
}
});
// and the 'jquery-private' module, in the
// jquery-private.js file:
define(['jquery'], function (jq) {
return jq.noConflict( true );
});
Run Code Online (Sandbox Code Playgroud)
我在优化后看到的问题是"jq"在"jquery-private.js"文件中未定义.
有任何想法吗?我试过设置jq = $但这似乎破坏了全局.
谢谢.
以下是我从RequireJS jQuery Instructions页面链接到jQuery CDN和优化示例以使用映射模块来使用你在原始问题中粘贴的noConflict部分所做的工作.
1 - 分叉样品
2 - www/js/lib/jquery-private.js使用此内容创建的文件
define(['jquery'], function (jq) {
return jq.noConflict( true );
});
Run Code Online (Sandbox Code Playgroud)
3 - 修改www/js/app.js为粘贴map部分,所以require.config现在看起来像这样:
requirejs.config({
"baseUrl": "js/lib",
"paths": {
"app": "../app",
"jquery": "//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min"
},
map: {
'*': { 'jquery': 'jquery-private' },
'jquery-private': { 'jquery': 'jquery' }
}
});
Run Code Online (Sandbox Code Playgroud)
4 - 修改www/js/app/main.js使用jqlocal而不是$(只是为了向自己证明它不是全局jQuery:
define(["jquery", "jquery.alpha", "jquery.beta"], function(jqlocal) {
jqlocal(function() {
jqlocal('body').alpha().beta();
});
});
Run Code Online (Sandbox Code Playgroud)
5 - 更改为tools文件夹并运行:
node r.js -o build.js
Run Code Online (Sandbox Code Playgroud)
6 - 更改为www-build已创建并运行的文件夹servedir(与哪个Web服务器无关,但这是我用于dev的内容)
7 - 浏览应用程序的本地地址和端口号(在我的情况下http://localhost:8000/app.html)并看到:
阿尔法就是去!
Beta是Go!
你可以在这里看到最终结果
| 归档时间: |
|
| 查看次数: |
3916 次 |
| 最近记录: |