简体   繁体   中英

what is the dojo AMD loading sequence?

I've tried looking for the order in which the dojo AMD loads it's modules, as I have seen in other questions that the order the modules are placed in the require[] block matter.

require(["dojo/dom",
"dijit/Dialog",
"dijit/form/Form", 
"dijit/form/TextBox",
"dijit/form/ValidationTextBox",
"dijit/form/Textarea",
"dijit/form/Button",
"dojox/validate/web",
"dojo/request",
"dojo/domReady!"],

would be an example of the require block...

Well... there is no real loading sequence (that's the Asynchronous in AMD). The dojo/domReady! module isn't loaded last either, its the module itself that delays the AMD callback being executed.

The only reason why someone would put the dojo/domReady! last, is because the returned object has no use. However, use or no use, if you need a specific module that you put after it, then you still have to add an extra parameter, for example:

require(["dojo/dom", "dojo/domReady!", "dojo/aspect"], function(dom, garbage, aspect) {

});

The code above works and will stay working even if you scramble the order of the modules, the only problem here is that, because we want the aspect module, we need to add a second parameter for the domReady module.

So, the common practice is to put all modules which you don't need (the returned object at least) at the end of the sequence, for example:

require(["dojo/dom", "dojo/aspect", "dojo/domReady!"], function(dom, aspect, garbage) {

});

And because parameters in JavaScript are optional, we can leave the ones we don't need away (if they're at the end), so the code above is the same as:

require(["dojo/dom", "dojo/aspect", "dojo/domReady!"], function(dom, aspect) {

});

And that's why it's often added as last parameter. Though there are other modules as well that are commonly placed at the end, for example:

  • All dojo/NodeList-* modules: These modules add extra functionality to the NodeList module. However, they are applied directly and the returned object of these modules is not useful.
  • dojo/parser : When you configure Dojo to automatically parse all HTML (using parseOnLoad ), then you don't necessarily need a reference to the parser module. However, you have to add it to make it work.
  • All widgets: If you're using declarative markup, you don't need an instance to the module itself, however, you have to load it to make it work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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