我正在尝试使用RequireJS 2.0.1 进行实验。我的目标是正确加载jQuery、Underscore 和Backbone。从原始的 RequireJS 文档中,我发现作者J. Burke在这个新版本中添加了一个名为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);
}
);
一切似乎都运行得很好,但我感觉自己缺少了什么。我知道有jQuery和Underscore的 AMDed 版本,但如果设置如此简单,我不明白为什么我应该使用它们。
那么,这个设置是正确的,还是我漏掉了什么?
json2
怎么样? 我们也需要它吗? - Henry