如果我正在制作一个JavaScript库供一般使用,我应该如何处理RequireJS支持?
据我所知,使您的代码更多或更少地符合RequireJS使得没有RequireJS就无法使用.那我为什么要那样做?
没有要求的人如何使用此代码?
有没有办法支持两者而不分叉/分支?我应该提供垫片代码吗?
我能正确理解吗?
我试图通过使用require在noConflict模式下加载jquery
require.config({
paths: {
'jquery': 'libs/jquery-req',
underscore: 'libs/underscore',
backbone: 'libs/backbone'
},
shim: {
jquery: {
init: function() {
console.log('jq init');
var jq = this.jQuery.noConflict(true);
jq.support.cors = true;
return jq;
},
exports: '$'
},
backbone: {
deps: ['underscore', 'jquery'],
init: function() {
console.log('b init');
},
exports: 'Backbone'
},
underscore: {
exports: '_'
}
}
});
Run Code Online (Sandbox Code Playgroud)
并使用它:
define([
'jquery',
'underscore',
'backbone'
], function(jq, _, Backbone) {
console.log(jq);
var initialize = function() {
// Router.initialize();
};
return {
initialize: initialize
};
});
Run Code Online (Sandbox Code Playgroud)
不幸的是,似乎从未调用过shim.jquery.init函数.有人可以帮我理解为什么吗?当我重命名jquery时,有什么奇怪的 …
我正在使用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 …Run Code Online (Sandbox Code Playgroud)