Configuring Dojo with dojoConfig
The dojoConfig
object (formerly djConfig
) allows you to set options and default behavior for various aspects of the toolkit. In this tutorial we'll explore what's possible and how you can put dojoConfig to use in your code.
dojoConfig
(前djConfig
)允许您设置选项,并默认行为该工具包的各个方面。在本教程中我们将探讨什么是可能的,你可以如何将dojoConfig在你的代码中使用。
Introduction
The dojoConfig
object (known as djConfig
prior to Dojo 1.6) is the primary mechanism for configuring Dojo in a web page or application. It is referenced by the module loader, as well as Dojo components with global options. It can further be used as a configuration point for custom applications, if desired.
The old object name of djConfig
is deprecated, but any existing code using it will continue to work up until 2.0. At the time of writing, most documentation still uses djConfig
; the two names are directly equivalent, but we'll adopt and encourage use of the new dojoConfig
name from here on.
介绍
dojoConfig
对象(被称为djConfig
先前Dojo1.6)是用于在网页或应用配置Dojo的主要机制。它是由模块加载,以及与全局选项Dojo组件被引用。如果需要的话它可以进一步被用作配置点为自定义应用程序。
旧对象名称djConfig
已经过时,但之前使用任何现有代码将继续工作,直到2.0。在写这篇文章的时候,大部分的文档仍然使用djConfig
; 这两个名字都直接等同,但我们会采取并鼓励使用新的dojoConfig
名字从这里开始。
Getting Started
Let's run through some quick examples to see how dojoConfig
works in practice. First, a programmatic example of setting dojoConfig
directly:
入门
让我们通过一些简单的例子跑,看看如何dojoConfig
在实践中运作。首先,直接设定的程序例子dojoConfig
<!-- set Dojo configuration, load Dojo -->
<script>
dojoConfig= {
has: {
"dojo-firebug": true
},
parseOnLoad: false,
foo: "bar",
async: true
};
</script>
<script src="//ajax.googleapis.com/ajax/libs/dojo/1.10.4/dojo/dojo.js"></script>
<script>
// Require the registry, parser, Dialog, and wait for domReady
require(["dijit/registry", "dojo/parser", "dojo/json", "dojo/_base/config", "dijit/Dialog", "dojo/domReady!"]
, function(registry, parser, JSON, config) {
// Explicitly parse the page
parser.parse();
// Find the dialog
var dialog = registry.byId("dialog");
// Set the content equal to what dojo.config is
dialog.set("content", "<pre>" + JSON.stringify(config, null, "\t") + "```");
// Show the dialog
dialog.show();
});
</script>
<!-- and later in the page -->
<div id="dialog" data-dojo-type="dijit/Dialog" data-dojo-props="title: 'dojoConfig / dojo/_base/config'"></div>
Notice that dojoConfig
is defined in a script block before dojo.js is loaded. This is of paramount importance—if reversed, the configuration properties will be ignored.
In this example, we have set three flags: parseOnLoad: false
, has
(dojo-firebug
sub-property), and async: true
. Additionally, a custom property has been specified: foo: "bar"
. For this demo, a dijit/Dialog
has been placed in the page. Code that runs from within the require
callback converts the value of dojo.config
to JSON and places it into the dialog for review. Among the results are our properties: parseOnLoad
, has
, and foo
. But there are also a few others, which are related to the fact that the demo page uses the cross-domain, Google-CDN-hosted version of Dojo 1.10.
It is important to note the distinction between dojoConfig
and dojo/_base/config
. dojoConfig
is purely for input purposes—this is how we communicate configuration parameters to the loader and modules. During the bootstrap process,dojo/_base/config
is populated from these parameters for later lookup by module code.
注意到dojoConfig
是在脚本块定义 之前的dojo.js被加载。这是极为重要的,如果颠倒,配置属性将被忽略。
在这个例子中,我们已经设置三个标志:parseOnLoad: false
,has
(dojo-firebug
子属性),和async: true
。此外,自定义属性已指定:foo: "bar"
。对于这个演示,一个dijit/Dialog
已被放置在页面中。从内部运行的代码require
回调转换价值dojo.config
为JSON并将其放入对话框进行审查。在这些结果是我们的属性:parseOnLoad
,has
,和foo
。但也有其他的,这是关系到演示页面使用跨域,Dojo1.10的谷歌-CDN托管版本的事实。
是很重要的去注意区分之间dojoConfig
和dojo/_base/config
, dojoConfig
纯粹是为了输入的目的,这是我们沟通的配置参数,加载程序和模块。在引导过程中,dojo/_base/config
从这些参数由模块代码后查找填充。
这里的书面声明同样的例子:
Here's the same example written declaratively:
<script src="//ajax.googleapis.com/ajax/libs/dojo/1.10.4/dojo/dojo.js"
data-dojo-config="has:{'dojo-firebug': true}, parseOnLoad: false, foo: 'bar', async: 1">
</script>
In this case, we use the same data-dojo-config
attribute on the Dojo script
element that you've seen in other tutorials and examples. This is entirely functionally equivalent to the previous example. In both cases, the config options we provide are ultimately mixed into the dojo/_base/config
object, where they can be retrieved immediately after the bootstrapping process that takes place as dojo.js
loads.
You can confirm this by setting some new values in dojoConfig
, and checking the dojo.config
object in the console. So,dojoConfig
is a generic configuration property-bag for Dojo. Lets see what options there are and how we can use them.
在这种情况下,我们使用相同data-dojo-config的道场的属性script,你已经在其他的教程和例子可见例子。这完全在功能上等效前面的例子。在这两种情况下,我们提供了配置选项最终混入 dojo/_base/config对象,在那里他们可以立即自举过程,发生作为后进行检索dojo.js加载。
您可以通过设置一些新的价值观证实了这一点dojoConfig,并检查dojo.config在控制台对象。所以,dojoConfig是一个通用的配置属性,Dojo。让我们来看看哪些选项还有以及我们如何使用它们。
has() Configuration
One of the major features added in Dojo 1.7+ was the use of the has() pattern for feature detection. We can specify features for the has() feature set in dojoConfig
, by including an object hash of features as the value of the has
property. This feature set is now used for determining certain supported capabilities in Dojo. For example, we could disable the amd factory scan (scanning the module for CommonJS require(module) statements to load as deps) with:
has()配置
在Dojo1.7+上其中的主要的特征之一是使用了has()模式去特征察觉。在dojoConfig
中,我们可以在has()设置指定的功能。通过包括功能的对象哈希作为价值has
属性。此特性集,现在用于Dojo确定一定的支持能力。例如,我们可以禁用AMD工厂扫描(扫描模块CommonJS的要求(模块)语句来加载为DEPS)有:
<script>
dojoConfig = {
has: {
"dojo-amd-factory-scan": false
}
};
</script>
Debug/Firebug Lite Configuration
You may be familiar by now with the isDebug
config flag from other tutorials or usage of Dojo in versions prior to Dojo 1.7, to explicitly enable debug information. In Dojo 1.7+, this is now also specified with a has() feature at a higher level of granularity. To enable debugging assistance with Firebug Lite for older versions of Internet Explorer, we can set the dojo-firebug feature (isDebug can still be used to load this, but using the feature will load earlier in the loading cycle in async mode). If you have Firebug or another console available and open, it does nothing. But if you don't have a console, it will load Dojo's version of Firebug Lite, and create the console UI at the bottom of the page. This can be handy when debugging in earlier versions of IE or other browsers without nice developer tools.
To enable debugging messages for deprecated and experimental features, we can set dojo-debug-messages to true (this defaults to false, unless you have set isDebug). if this feature is set to false
, these warnings will be suppressed. For example, to enable a developer console (browser provided or use Firebug Lite) and log debugging messages:
Debug/ Firebug的精简版配置
你可能已经熟悉了与isDebug
其他教程或以前版本中的Dojo的使用配置标志Dojo1.7,显式地启用调试信息。在道场1.7+,现在还以更高的粒度级别用has()特性指定。。为了让使用Firebug精简版旧版本的Internet Explorer的调试帮助,我们可以设置道场-萤火虫功能(isDebug仍然可以用来加载这一点,但使用该功能将在异步模式下早些时候在加载循环负载)。如果你有萤火虫或其他控制台提供开放的,它什么都不做。但是,如果你没有一个控制台,它会加载Dojo的版本Firebug的精简版,并在页面的底部创建控制台UI。在早期版本的IE或其他浏览器,而不好的开发工具调试时,这是很方便的。
要启用过时,实验功能调试信息,我们可以设置道场调试的消息为真(此默认为false,除非你已经设置isDebug)。如果此功能设置为false
,这些警告会被抑制。例如,为了使开发者控制台(浏览器提供或使用Firebug精简版)和日志调试信息: