dojo要求功能的目的是什么? [英] What is the purpose of function in the dojo require?

查看:146
本文介绍了dojo要求功能的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$ {code} //加载dom模块
require([dojo / dom],function(dom){
});

我知道这个函数在dom模块加载时被调用,但我不清楚什么作为函数内的代码。这是我页面上所有JavaScript代码的容器吗?

解决方案

该功能是回调,AMD加载程序将调用它已经加载了您需要的所有模块。



如果我有

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

//做某些声明并准备好

});

AMD正在加载并声明。这可能需要AMD对服务器进行异步调用。一旦AMD加载了这些模块,它会调用您传递到 require 方法的函数。



一个href =https://stackoverflow.com/questions/12922618/dojo-builds-what-now/12922801#12922801> Dojo Builds ...?现在呢?有一些关于AMD API的细节。






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

 < script type =text / javascript> 
require([dojo / _base / ready,dojo / _base / declare],function(ready,declare){
//做某事与declare和ready
});
< / script>

< script type =text / javascript>
require([dojo / _base / ready,dojo / _base / declare,dijit / form / Button],
函数(ready,declare,Button){
/ /假设这是要执行的第二个语句,AMD将
//意识到准备和声明以前已经被加载,
//所以它将使用以前加载的模块,加载Button模块
//然后执行回调

//使用declare,ready和Button
}执行某些操作;
< / script>


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

解决方案

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>

这篇关于dojo要求功能的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆