requirejs.config() 是做什么的?

What does requirejs.config() do?

我无法理解 requirejs.config() 函数。

requirejs.config({
    paths: {
        'text': '../lib/require/text',
        'durandal':'../lib/durandal/js',
        'plugins' : '../lib/durandal/js/plugins',
        'transitions' : '../lib/durandal/js/transitions',
        'knockout': '../lib/knockout/knockout-3.1.0',
        'bootstrap': '../lib/bootstrap/js/bootstrap',
        'jquery': '../lib/jquery/jquery-1.9.1'
    },
    shim: {
        'bootstrap': {
            deps: ['jquery'],
            exports: 'jQuery'
       }
    }
});

这个函数有什么作用?请不要指导我查看文档,因为我已经阅读了它,但仍然觉得它很混乱。我需要一个简单的解释来说明这个函数的作用。

这些脚本是异步加载的吗?

它为脚本路径创建别名 ant 告诉如何在加载时解释 bootstrap(非 AMD 脚本)。尚未加载任何内容。你必须要求:

// we load two dependencies here
// `knockout` and `bootstrap` are expanded to values in config
// .js added to values
// callback function called when all dependencies are loaded
require(['knockout', 'bootstap'], function(Knockout, $) {
    // jQuery is passed to this function as a second parameter
    // as defined in shim config exports
});

路径 类似于declarations/definitions。例如,

jquery: '../bower_components/jquery/dist/jquery',

您稍后可以按如下方式加载此库。

define([
  'jquery'
], function (jquery) {
   //initialize or do something with jquery
}

您不必指定库的绝对路径。

shim 中,您将定义依赖项。例如,

paths: {
  template: '../templates',
  text:  '../bower_components/requirejs-text/text',
  jquery: '../bower_components/jquery/dist/jquery',
  backbone: '../bower_components/backbone/backbone',
  underscore: '../bower_components/underscore/underscore',
  Router: 'routes/router'
},
shim: {
  'backbone': ['underscore', 'jquery'],
  'App': ['backbone']
}

这里backbone依赖于下划线jquery.所以这两个库将在 backbone 开始加载之前加载。同样的,App会在backbone加载后加载。

如果您熟悉 backbone 和 express,您可能会发现此 repo 很有用。

https://github.com/sohel-rana/backbone-express-boilerplate