dojo之配置dojoconfig

官方教程:Configuring Dojo with dojoConfig
例子:

<-- set Dojo configuration, load Dojo -->
<script>
    dojoConfig= {
        has: {
            "dojo-firebug"true
        },
        parseOnLoad: false,
        foo: "bar",
        async: true,
        aliases:[
            ["ready", "dojo/domReady"],
            ["registry","dijit/registry"],
            ["dialog","dijit/Dialog"],
            ["parser","dojo/parser"]
        ],
        packages: [{
            name: "js",
            location: "/js"
        }],
        
locale: location.search.match(/locale=([w-]+)/) ? RegExp.$1 : "en-us"
    };
</script>
<script src="http://blog.163.com/mqsy_yj/blog/http://ajax.googleapis.com/ajax/libs/dojo/1.7.1/dojo/dojo.js"></script>
 
<script>
// Require the registry, parser, Dialog, and wait for domReady
require(["registry""parser""dialog""ready!"], function(registry, parser) {
    // Explicitly parse the page
    dojo.parser.parse();
    // Find the dialog
    var dialog = registry.byId("dialog");
    // Set the content equal to what dojo.config is
    dialog.set("content""<pre>" + dojo.toJson(dojo.config, true) + "</pre>");
    // 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.config'"></div>

dojoConfig用于设置一些在Dojo运行时的选项和默认的行为方式。
在上面的代码中,首先定义dojoConfig设置一些属性,然后加载dojo.js,如果这个过程反过来,那dojoConfig的配置则无效。
        data-dojo-config="has:{'dojo-firebug': true}, parseOnLoad: false, foo: 'bar', async: true">
</script>
这是另一种写法,等同于前一种。前一种是写在一个单独的script块中,后一种是作为script标签的一个属性。个人认为前一种比较好,如果需要配置的内容比较多时,前一种会更清楚直观,容易理解。
下面说一些配置项。
has()用来设置一些Dojo支持的系统特性。
has: {
    "dojo-firebug"true,//加载Dojo版的Firebug调试环境,如果浏览器没有自带调试工具,可以用这个
    "dojo-debug-messages"true//显示调试信息,针对于一些废弃的或测试中的功能特性在运行时的信息
}
另外还有debugContainerId,用于在页面显示debug控制台界面的地方;popup,在弹出窗口中显示debug控制台,而不是在当前页面中。


Loader Configuration
加载时一些常用选项
packages: [{
    name: "myapp",
    location: "/js/myapp"
}]
//提供包名及其路径

aliases: [
    // [alias name, true name]
    ["cookie""dojo/cookie"]
]
//设置别名

async:true/false/
legacyAsync 
//是否异步加载

parseOnLoad:true/false 
//是否在DOM和所有初始化完成后由dojo.parser解析页面


本地化与国际化
locale: location.search.match(/locale=([w-]+)/) ? RegExp.$1 : "en-us"

上面代码会在地址中查找参数locale=xx,如http://dojotoolkit.org/documentation/tutorials/1.7/dojo_config/demo/localeConfig.html?locale=zh,这里参数locale=zh

require(["dojo/date/locale""dijit/Dialog""dojo/i18n""dojo/domReady!"]function(locale, Dialog){
            var now = new Date();
            var dialog = new Dialog({
                id: "dialog",
                title: "Today: " + locale.format(now,{
                    formatLength:"full",
                    selector:"date"
                })
            }).placeAt(dojo.body());
            dialog.startup();
            dialog.set("content","<pre>" + dojo.toJson(dojo.config,true) + "</pre>");
            dialog.show();
        });

上面代码中将日期格式进行本地化放在Dialog的标题栏中。如果没有找到locale=xx,则默认为en-us。

转载自:http://www.cnblogs.com/tiandi/p/3415897.html

原文地址:https://www.cnblogs.com/yangzhilong/p/3785439.html