【发布时间】:2012-06-07 15:59:42
【问题描述】:
我正在尝试使用 RequireJS 2.0.1。我的目标是正确加载 jQuery、Underscore 和 Backbone。从最初的 RequireJS doc 我发现作者 J. Burke 添加了(在这个新版本中)new config option called shim。
然后我把这些东西写在这里:
index.html
<!DOCTYPE html>
<html>
<head>
<title>Testing time</title>
<script data-main="scripts/main" src="scripts/require.js"></script>
</head>
<body>
<h1>Testing time</h1>
</body>
</html>
scripts/main.js
requirejs.config({
shim: {
'libs/jquery': {
exports: '$'
},
'libs/underscore': {
exports: '_'
},
'libs/backbone': {
deps: ['libs/underscore', 'libs/jquery'],
exports: 'Backbone'
}
}
});
define(
['libs/jquery', 'libs/underscore', 'libs/backbone'],
function (jQueryLocal, underscoreLocal, backboneLocal) {
console.log('local', jQueryLocal);
console.log('local', underscoreLocal);
console.log('local', backboneLocal);
console.log('global', $);
console.log('global', _);
console.log('global', Backbone);
}
);
一切似乎都很好,但我觉得我错过了一些东西,我知道有 AMDed 版本的 jQuery 和 Underscore,但如果设置如此简单,我不会'不明白我为什么要使用它们。
那么,这个设置是正确的还是我遗漏了什么?
【问题讨论】:
-
json2怎么样?我们也需要那个吗?
标签: javascript requirejs shim