【发布时间】:2015-05-12 08:03:22
【问题描述】:
我是一个受虐狂,所以我决定改变我在项目中加载 require.js 的方式。我所做的只是从这个出发(使用 Modernizr.load)......
<script type="text/javascript">
function requireJsConfig () {
requirejs.config({
'baseUrl': '{{ STATIC_URL }}js',
'paths': {
'jquery': 'libraries/jquery',
'backbone': 'libraries/backbone',
'underscore': 'libraries/underscore',
'jquery.cookie': 'libraries/jquery-cookie',
'tinymce': 'tinymce/tinymce.min',
'react' : 'libraries/react',
'history': 'history'
},
waitSeconds: 30,
shim: {
'jquery' : 'jquery',
'underscore' : {
exports: '_'
},
'backbone': {
deps: ['underscore', 'jquery'],
exports: 'Backbone'
},
'jquery.cookie': {
deps: ['jquery'],
exports: '$.cookie'
},
'tinymce': {
deps: ['jquery'],
exports: 'tinymce'
}
}
});
define('static_url', [], "{{ STATIC_URL }}");
window.static_url = "{{ STATIC_URL }}";
define('PUBNUB_SUBSCRIBE_KEY', [], "{{ PUBNUB_SUBSCRIBE_KEY }}");
require(["teacher"]);
};
Modernizr.load({
load: '{{ STATIC_URL }}js/require.js',
complete: function() {
requireJsConfig();
}
});
</script>
...到这个(不使用 Modernizr.load)...
<script type="text/javascript" src="{{ STATIC_URL }}js/require.js"></script>
<script type="text/javascript">
function requireJsConfig () {
requirejs.config({
'baseUrl': '{{ STATIC_URL }}js',
'paths': {
'jquery': 'libraries/jquery',
'backbone': 'libraries/backbone',
'underscore': 'libraries/underscore',
'jquery.cookie': 'libraries/jquery-cookie',
'tinymce': 'tinymce/tinymce.min',
'react' : 'libraries/react',
'history': 'history'
},
waitSeconds: 30,
shim: {
'jquery' : 'jquery',
'underscore' : {
exports: '_'
},
'backbone': {
deps: ['underscore', 'jquery'],
exports: 'Backbone'
},
'jquery.cookie': {
deps: ['jquery'],
exports: '$.cookie'
},
'tinymce': {
deps: ['jquery'],
exports: 'tinymce'
}
}
});
define('static_url', [], "{{ STATIC_URL }}");
window.static_url = "{{ STATIC_URL }}";
define('PUBNUB_SUBSCRIBE_KEY', [], "{{ PUBNUB_SUBSCRIBE_KEY }}");
require(["teacher"]);
};
requireJsConfig();
</script>
...现在我明白了:
未捕获的错误:不匹配的匿名 define() 模块:函数 (){return r}
FWIW,这是发生错误的地方(在 require.js 中):
function intakeDefines() {
var args;
//Any defined modules in the global queue, intake them now.
takeGlobalQueue();
//Make sure any remaining defQueue items get properly processed.
while (defQueue.length) {
args = defQueue.shift();
if (args[0] === null) {
return onError(makeError('mismatch', 'Mismatched anonymous define() module: ' + args[args.length - 1]));
} else {
//args are id, deps, factory. Should be normalized by the
//define() function.
callGetModule(args);
}
}
}
顺便说一句,我已经验证了these 都不适用。
为什么这个看似无害的变化会导致我的项目中的核弹爆炸? (是的,一枚真正的核弹引爆了……仿佛数以百万计的声音突然惊恐地呼喊,又突然沉默了……)
【问题讨论】:
标签: javascript requirejs modernizr