简体   繁体   English

此requirejs / amd模块中的“ order!”指令是什么?

[英]What is the “order!” directive for in this requirejs/amd module?

I'm looking into what appears to be a case of javascript loading out of order in a legacy application. 我正在研究在旧版应用程序中javascript无法正常加载的情况。 The application uses Require.js to load several modules, and one of our company's modules is executing prior to its dependencies being loaded. 该应用程序使用Require.js加载多个模块,并且在加载依赖项之前,我们公司的一个模块正在执行。

My experience with Require.js and AMDs is very limited, and in researching I noted that in some areas dependencies are prefixed with an order! 我对Require.js和AMD的经验非常有限,在研究中,我注意到在某些方面,依赖项以order!为前缀order! string, such as: 字符串,例如:

define(['order!jquery', ...

Whereas in other areas the prefix isn't used: 而在其他区域则不使用前缀:

define(['jquery', ...

So far I can't find documentation of this directive. 到目前为止,我找不到该指令的文档。 What's its effect? 有什么作用?

Full information copied from here 这里复制的完整信息

Normally RequireJS loads and evaluates scripts in an undetermined order. 通常,RequireJS会以不确定的顺序加载和评估脚本。 However, there are some traditional scripts that depend on being loaded in a specific order. 但是,有些传统脚本依赖于以特定顺序加载。 For those cases you can use the order plugin. 在这种情况下,您可以使用订单插件。 Download the plugin and put it in the same directory as your app's main JS file. 下载插件,并将其放在应用程序的主JS文件所在的目录中。 Example usage: 用法示例:

 require(["order!one.js", "order!two.js", "order!three.js"], function () { //This callback is called after the three scripts finish loading. }); 

Scripts loaded by the order plugin will be fetched asynchronously, but evaluated in the order they are passed to require, so it should still perform better than using script tags in the head of an HTML document. 由订单插件加载的脚本将以异步方式获取,但会按照传递给它们的顺序对其进行评估,因此与在HTML文档的开头使用脚本标签相比,它的性能仍然要好。

The order plugin is best used with traditional scripts. 订单插件最好与传统脚本一起使用。 It is not needed for scripts that use define() to define modules. 使用define()定义模块的脚本不需要此脚本。 It is possible to mix and match "order!" 可以混合搭配“订单”! dependencies with regular dependencies, but only the "order!" 具有常规依赖关系的依赖关系,但只有“顺序!” ones will be evaluated in relative order to each other. 将会以彼此相对的顺序进行评估。

Notes: 笔记:

  • The order! 命令! plugin only works with JavaScript files that are cacheable by the browser. 该插件仅适用于浏览器可缓存的JavaScript文件。 If the JS file has headers that do not allow the browser to cache the file, then the order of scripts will not be maintained. 如果JS文件的标头不允许浏览器缓存文件,则脚本的顺序将不被维护。
  • Do not use the order! 不要使用命令! plugin to load other plugin-loaded resources. 插件以加载其他插件加载的资源。 For instance. 例如。 'order!cs!my/coffescript/module' is not recommended. 不建议使用'order!cs!my/coffescript/module' You will get errors in some versions of IE and WebKit. 您将在某些版本的IE和WebKit中得到错误。 This is due 这个到期了
    to the workarounds the order plugin needs to do for those browsers 订单插件需要为那些浏览器执行的解决方法
    to ensureordered execution. 确保有序执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM