dojo.require_dojo.require()之美

dojo.require

I've been working full time with Dojo for the past four months and one of my favorite parts of the toolkit is the dojo.require system.  The dojo.require system allows you to asynchronously request Dojo modules within the current page without needing to adjust your core Dojo build or needing to go out and download the given plugin.  dojo.require is quite comprehensive but I wanted to give you a taste of dojo.require and how it works on a very basic level.

在过去的四个月中,我一直在与Dojo一起工作,而我最喜欢的工具包之一就是dojo.require系统。 dojo.require系统允许您在当前页面中异步请求Dojo模块,而无需调整您的核心Dojo构建或需要外出并下载给定的插件。 dojo.require非常全面,但我想向您介绍dojo.require以及它在非常基本的水平上如何工作。

步骤1:从CDN提取Dojo (Step 1:  Pull Dojo From CDN)


<!-- pull from Google -->
<script src="http://ajax.googleapis.com/ajax/libs/dojo/1.5/dojo/dojo.xd.js" type="text/javascript"></script>
<!-- OR pull from AOL -->
<script src="http://o.aolcdn.com/dojo/1.5/dojo/dojo.xd.js" type="text/javascript"></script>


You can pull the base Dojo JavaScript file from Google or AOL.  This file is very small and loads extremely fast from CDN.

您可以从Google或AOL中提取基本的Dojo JavaScript文件。 该文件非常小,并且可以从CDN加载得非常快。

步骤2:dojo.require (Step 2:  dojo.require)

As mentioned earlier, the dojo.require method asynchronously requests Dojo classes from a module path.  Dojo's smart enough to be able to request its classes from CDN even though it's cross-domain.  You can change the module paths if you'd like but that's out of the scope of this post.  Let's say I want to use Dojo's behavior class within my page.  The first step is "requiring" it:

如前所述,dojo.require方法从模块路径异步请求Dojo类。 Dojo非常聪明,即使它是跨域的,也能够从CDN请求其类。 您可以根据需要更改模块路径,但这超出了本文的范围。 假设我想在页面中使用Dojo的行为类。 第一步是“要求”它:


dojo.require('dojo.behavior');


That require statement fires the request for that class from the CDN.  Of course, you can't do anything with the functionality of that class until it has loaded so you need to add a dojo.ready wrapper which doesn't execute until all requires have loaded and the DOM is ready:

该require语句从CDN触发对该类的请求。 当然,在该类的功能加载之前,您无法做任何事情,因此您需要添加一个dojo.ready包装器,该包装器在所有要求都已加载并且DOM准备就绪之前不会执行:


//equivalent to jQuery's document.ready and MooTools' window.addEvent('domready')
dojo.ready(function() {
	
	//this only executes when my "requires" have loaded!
	dojo.behavior.add({
		'a.alert': {
			onclick: function(e) {
				alert('You clicked me!');
			}
		}
	})
	
});


Boom!  The behavior class has loaded and you're now ready to use it!

繁荣! 行为类已加载,您现在可以使用它了!

Another cool part about dojo.require is that since each class must define the modules it requires, dependencies are automatically required when you request a class.  For example, let's pretend I want to use a class from DojoX charting library.  I manually require once class, but Dojo's smart enough to figure out I need many more than that:

关于dojo.require的另一个很酷的部分是,由于每个类都必须定义所需的模块,因此在您请求一个类时会自动需要依赖项。 例如,假设我们要使用DojoX图表库中的类。 我手动要求上一次课,但是Dojo很聪明,以至于我需要的还不止于此:


//I manually type this...
dojo.require('dojox.charting.widget.Chart2D');
//...but Dojo internally knows to also require more classes that help charting...


Firebug can tell you exactly which dependency classes get pulled from CDN:

Firebug可以准确告诉您从CDN中提取了哪些依赖项类:

dojo.require

Dojo's require system is absolutely brilliant;  it speeds up development ten-fold.  When it comes to production, however, it's best to use Dojo's build system to create one static JavaScript build for speed and caching purposes.  For development, however, dojo.require is a Godsend!

Dojo的需求系统非常出色; 它可以将开发速度提高十倍。 但是,在生产环境中,最好使用Dojo的构建系统来创建一个静态JavaScript构建,以实现速度和缓存目的。 对于开发而言,dojo.require是天赐之物!

翻译自: https://davidwalsh.name/dojo-require

dojo.require

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值