0

我试图了解 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();
});
4

1 回答 1

0

Backbone 由 shim 配置导出为全局,因此无需定义依赖项,因为它已经在全局命名空间中可用。

有关详细信息,请参阅http://requirejs.org/docs/api.html#config-shim

于 2012-09-28T15:34:42.293 回答