2013-02-28 96 views
1
// Load the dom module 
require(["dojo/dom"], function(dom){ 
}); 

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

+0

还请参阅[上requirejs.org文章](http://requirejs.org/docs/why.html) – McDowell 2013-03-01 11:13:28

回答

6

该函数是回调函数,当加载器加载所有你需要的模块时会调用它。

如果我有

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

如果两个模块被加载那么只有一个功能将被执行? – 2013-02-28 15:45:29

+0

是的。你可以阅读require方法调用,因为“这些是我需要的模块,当你(AMD)加载它们时,这就是我想要做的(函数) – 2013-02-28 15:56:19

+0

但是我的页面有很多任务需要完成。我必须一次又一次地为不同的任务需要相同的模块吗? - sly_Chandan 5分钟前 – 2013-02-28 16:09:19