admin管理员组文章数量:1335624
I want to get rid of the global jQuery objects (window.$ and maybe also window.jQuery).
data-main:
require.config({
paths: {
"jquery": "jquery-2.0.0"
},
shim: {
"bootstrap": {
deps: ["jquery"]
},
"jquery": {
deps: [],
init: function() {
return this.jQuery.noConflict(true);
},
exports: "jQuery"
}
}
});
require(["jquery", "bootstrap"], function($) {
// ...
});
What's wrong with this code? "init" is never called.
I want to get rid of the global jQuery objects (window.$ and maybe also window.jQuery).
data-main:
require.config({
paths: {
"jquery": "jquery-2.0.0"
},
shim: {
"bootstrap": {
deps: ["jquery"]
},
"jquery": {
deps: [],
init: function() {
return this.jQuery.noConflict(true);
},
exports: "jQuery"
}
}
});
require(["jquery", "bootstrap"], function($) {
// ...
});
What's wrong with this code? "init" is never called.
Share Improve this question edited Jun 28, 2013 at 13:41 Steve P 19.4k7 gold badges72 silver badges96 bronze badges asked May 24, 2013 at 13:53 bertbert 4663 silver badges7 bronze badges2 Answers
Reset to default 4The recently-updated Use with jQuery page on the RequireJS site explains more about why this happens and what you can do to resolve it. Here is the relevant part:
jQuery registers itself as the global variables "$" and "jQuery", even when it detects AMD/RequireJS. The AMD approach advises against the use of global functions, but the decision to turn off these jQuery globals hinges on whether you have non-AMD code that depends on them. jQuery has a noConflict function that supports releasing control of the global variables and this can be automated in your require.config, as we will see later.
And if you want to suppress these global functions, you need to use a map
config to a noConflict wrapper module rather than a shim
config. As @tomaskirda pointed out, jQuery doesn't fire shim.init
for libraries that support AMD. The relevant code from the Use with jQuery page:
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 );
});
It is not called most likely because jQuery
implements AMD module and there is no need for shim.
本文标签: javascriptrequirejs jQuery noConflict shimWhy is init method never calledStack Overflow
版权声明:本文标题:javascript - require.js jQuery noConflict shim - Why is init method never called - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742391185a2466023.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论