javascript RequireJS 依赖顺序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31868403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
RequireJS order of dependencies
提问by Alexander Mills
if you have a RequireJS module like so:
如果你有一个像这样的 RequireJS 模块:
define(
[
'#patches',
'backbone',
'underscore',
'react',
'#allCollections',
'#allModels',
'app/js/routers/router',
'#allTemplates',
'#allControllers',
'#allRelViews'
],
function(){
var patches = arguments[0];
});
is there any way to know which dependency gets loaded first? In my case, '#patches' is a few window.X utility functions that I want to load before anything else. Do I need to configure this a different way to ensure this?
有没有办法知道首先加载哪个依赖项?就我而言,'#patches' 是一些我想先加载的 window.X 实用程序函数。我是否需要以不同的方式配置它以确保这一点?
(in my case "#' is just my own notation to denote a module whose path is predefined in my main config file)
(在我的例子中,“#”只是我自己的符号,用来表示一个模块,其路径在我的主配置文件中预定义)
回答by Fran?ois Richard
From the documentation : http://requirejs.org/docs/api.html#mechanics
从文档:http: //requirejs.org/docs/api.html#mechanics
"RequireJS waits for all dependencies to load, figures out the right order in which to call the functions that define the modules, then calls the module definition functions once the dependencies for those functions have been called. Note that the dependencies for a given module definition function could be called in any order, due to their sub-dependency relationships and network load order."
“RequireJS 等待所有依赖项加载,找出调用定义模块的函数的正确顺序,然后在调用这些函数的依赖项后调用模块定义函数。请注意,给定模块定义的依赖项由于它们的子依赖关系和网络加载顺序,可以按任何顺序调用函数。”
I think this may help: http://www.sitepoint.com/understanding-requirejs-for-effective-javascript-module-loading/(see "Managing the Order of Dependent Files")
我认为这可能会有所帮助:http: //www.sitepoint.com/understanding-requirejs-for-effective-javascript-module-loading/(参见“管理依赖文件的顺序”)
RequireJS uses Asynchronous Module Loading (AMD) for loading files. Each dependent module will start loading through asynchronous requests in the given order. Even though the file order is considered, we cannot guarantee that the first file is loaded before the second file due to the asynchronous nature. So, RequireJS allows us to use the shim config to define the sequence of files which need to be loaded in correct order. Let's see how we can create configuration options in RequireJS.
RequireJS 使用异步模块加载 (AMD) 来加载文件。每个依赖模块将按照给定的顺序通过异步请求开始加载。即使考虑了文件顺序,由于异步性质,我们也不能保证第一个文件在第二个文件之前加载。因此,RequireJS 允许我们使用 shim 配置来定义需要以正确顺序加载的文件序列。让我们看看如何在 RequireJS 中创建配置选项。
requirejs.config({
shim: {
'source1': ['dependency1','dependency2'],
'source2': ['source1']
}
});
Hope it helps
希望能帮助到你
EDIT: As said in comments, using Shim for AMD module is a bad idea, use only shim for non AMD modules and manage dependencies order there. For AMD module requirejs will manage the order of loading. A good link from the comments (thanks Daniel Tulp) ==> Requirejs why and when to use shim config
编辑:正如评论中所说,对 AMD 模块使用 Shim 是一个坏主意,仅对非 AMD 模块使用 shim 并在那里管理依赖项顺序。对于 AMD 模块,requirejs 将管理加载顺序。来自评论的一个很好的链接(感谢 Daniel Tulp)==> Requirejs 为什么以及何时使用 shim 配置