JavaScript/Dojo模块模式 - 如何调试?

djn*_*jna 5 javascript debugging dojo

我正在使用Dojo并使用Mastering Dojo中描述的"模块模式" .到目前为止,我可以看到这种模式是一种通用的,广泛使用的JavaScript模式.我的问题是:我们如何调试我们的模块?

到目前为止,我还没能说服Firebug向我展示我的模块的来源.Firebug似乎只显示用于执行工厂方法的dojo eval语句.因此,我无法单步执行我的模块源代码.我试过在我的模块代码中加入"调试器"语句,Firebug似乎正常停止,但没有显示源代码.

下面的示例代码.这只是一个足够复杂的例子,使调试的需要变得合理,它并不是有用的代码.

这页纸

<!--
  Experiments with Debugging
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
  "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head> 
    <title>console me</title>

    <style type="text/css">
      @import "../dojoroot/dojo/resources/dojo.css";
      @import "../dojoroot/dijit/themes/tundra/tundra.css";
      @import "edf.css";
    </style>    

    <script type="text/javascript" src="../dojoroot/dojo/dojo.js">
    </script>

    <script type="text/javascript" >
      dojo.registerModulePath("mytest", "../../mytest");

      dojo.require("mytest.example");

      dojo.addOnLoad(function(){
         mytest.example.greet();                     
      });
    </script>

  </head>
  <body class="tundra">
    <div id="bulletin">
      <p>Just Testing</p>
    </div>
  </body>
</html>
<!-- END: snip1 -->
Run Code Online (Sandbox Code Playgroud)

我想调试的java脚本

dojo.provide("mytest.example");
dojo.require("dijit.layout.ContentPane");

/**
 * define module
 */
(function(){
      //define the main program functions...
      var example= mytest.example;
      example.greet= function(args) {

          var bulletin = dojo.byId("bulletin");

          console.log("bulletin:" + bulletin);

          if ( bulletin) {
                var content = new dijit.layout.ContentPane({
                    id: "dummy",
                    region: "center"
                  });
                content.setContent('Greetings!');

                dojo._destroyElement(bulletin);
                dojo.place(content.domNode, dojo.body(), "first");
              console.log("greeting done");
          } else {
              console.error("no bulletin board");
          }           
      }
})(); 
Run Code Online (Sandbox Code Playgroud)

djn*_*jna 3

(我自己回答这个问题,因为这似乎是一个常见问题,其解决方案并不为人所知。)

看起来,只要 dojo 进行一点合作,就可以很好地调试 FireBug 中的 eval 代码。技巧是配置 dojo 以使用 debugAtAllCosts启用此类调试

<script type="text/javascript" src="/dojoroot/dojo/dojo.js"
        djConfig="parseOnLoad: true, debugAtAllCosts: true"></script>
Run Code Online (Sandbox Code Playgroud)

dojo Campus 中的debug对此进行了描述,其中还指出,出于性能原因,在生产中不建议使用此设置,并建议使用服务器端条件来控制是否启用此类调试的方法。