问题
// 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