What is the purpose of function in the dojo require?

こ雲淡風輕ζ 提交于 2019-12-11 08:31:53

问题


// Load the dom module
require(["dojo/dom"], function(dom){
});

I understand that the function is called when the dom module is loaded but I am not clear with what will be the code within the function. Is it a container for all the javascript code on my page?


回答1:


The function is callback, that the AMD loader will call when it has loaded all the modules that you require.

If I have

require(["dojo/_base/ready", "dojo/_base/declare"], function(ready, declare) {

  // do something with declare and ready

});

AMD is going to load ready and declare. This may require AMD to make an asynchronous call back to the server. Once AMD has loaded the modules, it calls the function you passed into the require method.

My answer at Dojo Builds...? What now? has some more details on the AMD API.


Answer to the question in the comment. The following two statements could be anywhere on the page.

<script type="text/javascript">
require(["dojo/_base/ready", "dojo/_base/declare"], function(ready, declare) {
   // do something with declare and ready
});
</script>

<script type="text/javascript">
require(["dojo/_base/ready", "dojo/_base/declare", "dijit/form/Button"], 
   function(ready, declare, Button) {
     // Assuming this is the second statement to be executed, AMD will 
     // realize that ready and declare have previously been loaded,
     // so it will use the previously loaded modules, load the Button module, 
     // and then execute the callback

     // do something with declare, ready, and Button
});
</script>


来源:https://stackoverflow.com/questions/15130450/what-is-the-purpose-of-function-in-the-dojo-require

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!