RequireJS依赖顺序 [英] RequireJS order of dependencies

查看:259
本文介绍了RequireJS依赖顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果你有这样的RequireJS模块:

if you have a RequireJS module like so:

define(
    [
        '#patches',
        'backbone',
        'underscore',
        'react',
        '#allCollections',
        '#allModels',
        'app/js/routers/router',
        '#allTemplates',
        '#allControllers',
        '#allRelViews'
    ],

function(){

   var patches = arguments[0];

});

有没有办法知道首先加载哪个依赖项?就我而言,'#patches'是一些window.X实用程序函数,我想在其他任何东西之前加载。我是否需要以不同的方式配置以确保这一点?

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?

(在我的情况下,#只是我自己的符号,表示其路径在我的预定义中的模块主配置文件)

(in my case "#' is just my own notation to denote a module whose path is predefined in my main config file)

推荐答案

从文档: http://requirejs.org/docs/api.html#mechanics

RequireJS等待所有人要加载的依赖项,找出调用定义模块的函数的正确顺序,然后在调用这些函数的依赖项后调用模块定义函数。请注意,由于它们的子依赖关系和网络加载顺序,可以按任何顺序调用给定模块定义函数的依赖关系。

"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."

我认为这可能会有所帮助: http://www.sitepoint.com/understanding- requirejs-for-effective-javascript-module-loading / (参见管理从属文件的顺序)

I think this may help: http://www.sitepoint.com/understanding-requirejs-for-effective-javascript-module-loading/ (see "Managing the Order of Dependent Files")

RequireJS使用异步模块加载(AMD)每个依赖模块将按给定顺序通过异步请求开始加载。即使考虑了文件顺序,由于异步性质,我们无法保证第一个文件在第二个文件之前加载。因此,RequireJS允许我们使用shim配置来定义需要按正确顺序加载的文件序列。让我们看看我们如何在RequireJS中创建配置选项。

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.config({
  shim: {
    'source1': ['dependency1','dependency2'],
    'source2': ['source1']
  }
});

希望有所帮助

编辑:如评论中所述,使用Shim for AMD模块是一个坏主意,仅使用垫片非AMD模块并管理依赖关系。
对于AMD模块,requirejs将管理加载顺序。
来自评论的好链接(感谢Daniel Tulp)==> 需要为什么以及何时使用shim配置

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

这篇关于RequireJS依赖顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆