重命名核心Dojo包

时间:2012-02-21 03:40:04

标签: javascript dojo

使用Dojo 1.6时,我可以“重命名”核心dojo包,如下所示:

<html>

<head>

<script type="text/javascript">
   djConfig = {
      baseUrl: 'https://ajax.googleapis.com/ajax/libs/dojo/1.6.1/dojo/',
      scopeMap: [
         [ 'dojo', 'myDojo' ]
      ]
   }
</script>
<script src="https://ajax.googleapis.com/ajax/libs/dojo/1.6.1/dojo/dojo.xd.js"></script>
</head>

<body>
<script type="text/javascript">
   if (typeof dojo != 'undefined') {
      document.write("Defined: dojo: " + dojo.version + '\n');
   }
   if (typeof myDojo != 'undefined') {
      document.write("Defined: myDojo: " + myDojo.version + '\n');
   }
</script>
</body>

</html>

以上打印“Defined:myDojo:1.6.1 ......”。我怎样才能在Dojo 1.7中完成同样的事情?在示例here之后,我尝试了这个测试,但无济于事:

<html>

<head>

<script type="text/javascript">
   dojoConfig = {
      baseUrl: 'dojo/1.7.2/dojo/',
      packages: [
         { name: 'myDojo',
           location: '../dojo',
           packageMap: {
              dojo: 'myDojo'
           }
         }
      ]
   }
</script>
<script src="dojo/1.7.2/dojo/dojo.js"></script>
</head>

<body>
<script type="text/javascript">
   if (typeof dojo != "undefined") {
      document.write("Defined: dojo: " + dojo.version + '\n');
   }
   if (typeof myDojo != "undefined") {
      document.write("Defined: myDojo: " + myDojo.version + '\n');
   }
</script>
</body>

</html>

打印“Defined:dojo:1.7.2 ......”。我错过了什么?

1 个答案:

答案 0 :(得分:2)

要在使用源版本时重新映射全局变量,您需要将dojoConfig.async设置为真实的,然后您需要加载'myDojo'包。

http://jsfiddle.net/neonstalwart/44e56/是以下代码的工作示例:

<script type="text/javascript">
    // configure dojo
    dojoConfig = {
        async: 1,
        packages: [
            {
                name: 'myDojo',
                location: '../1.7.2',
                // re-map globals to different names
                packageMap: {
                    dojo: 'myDojo'
                }
            }
        ],
        deps: ['myDojo'],
        callback: function () {
            if (typeof dojo != "undefined") {
                alert("Defined: dojo: " + dojo.version + '\n');
            }
            if (typeof myDojo != "undefined") {
                alert("Defined: myDojo: " + myDojo.version + '\n');
            }
        }
    };
</script>
<script type="text/javascript" src="https://raw.github.com/dojo/dojo/1.7.2/dojo.js"></script>