我正在尝试理解TodoMVC的Backbone + RequireJS版本。我不明白为什么在main.js文件中,主干包不是依赖?
// Require.js allows us to configure shortcut alias
require.config({
// The shim config allows us to configure dependencies for
// scripts that do not call define() to register a module
shim: {
'underscore': {
exports: '_'
},
'backbone': {
deps: [
'underscore',
'jquery'
],
exports: 'Backbone'
}
},
paths: {
jquery: 'lib/jquery/jquery.min',
underscore: '../../../assets/lodash.min',
backbone: 'lib/backbone/backbone',
text: 'lib/require/text'
}
});
require([
'views/app',
'routers/router' // <--views/app.js and routers/router.js are the only dependencies
], function( AppView, Workspace ) {
// Initialize routing and start Backbone.history()
new Workspace();
Backbone.history.start(); // <--Here we use Backbone variable. Where do the writer tell the page about the backbone dependency??
// Initialize the application view
new AppView();
});
主干通过填充程序配置导出为全局,因此无需定义依赖项,因为它已在全局命名空间中可用。
有关更多详细信息,请参阅 http://requirejs.org/docs/api.html#config-shim