dojo中函数的用途是什么?

时间:2013-02-28 08:06:29

标签: dojo

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

我知道在加载dom模块时会调用该函数,但我不清楚函数中的代码是什么。它是我页面上所有javascript代码的容器吗?

1 个答案:

答案 0 :(得分:6)

该函数是回调函数,AMD加载器在加载了您需要的所有模块时将调用它。

如果我有

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

  // do something with declare and ready

});
AMD将准备加载并声明。这可能需要AMD对服务器进行异步调用。一旦AMD加载了模块,它就会调用您传递给require方法的函数。

我在Dojo Builds...? What now?的回答有关于AMD API的更多详细信息。


回答评论中的问题。以下两个陈述可以在页面的任何位置。

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