Dojo Debug to return a String

For my iPhone inspection I needed to find a way to parse through the objects and dump out all the data into a string that I could pass back via IO to the server where I could save it (this was to save me only having access to the iPhone client side dump on the iPhone (not the best place).

All of the dojo debuggers, have features that don’t allow you to set the value of a variable to the debug output, so I went into debug.js in the dojo 0.4.3 source file and stole the debugShallow code and converted it so it returned the debug statement.

  1.  
  2. dojo.debugReturn = function(/*Object*/obj){
  3.     if (!djConfig.isDebug) { return; }
  4.     var debug =‘————————————————————/r/n’;
  5.     debug =+ ‘Object: ‘+obj;
  6.     var props = [];
  7.     for(var prop in obj){
  8.         try {
  9.             props.push(prop + ‘: ‘ + obj[prop]);
  10.         } catch(E) {
  11.             props.push(prop + ‘: ERROR - ‘ + E.message);
  12.         }
  13.     }
  14.     props.sort();
  15.     for(var i = 0; i < props.length; i++) {
  16.         debug += props[i]+\r\n;
  17.     }
  18.     debug+=‘————————————————————’;
  19.     return debug;
  20. }
  21.  

This then meant that I could do:

  1.  
  2. <script type="text/javascript" charset="utf-8">
  3.   var testSubject = <?= $_GET[’subject’]; ?>;
  4.   var object = dojo.debugReturn(testSubject);
  5.  
  6.   dojo.io.bind({
  7.       method: "post",
  8.       url: "save.php",
  9.       content: {
  10.         testSubject: testSubject,
  11.         innerHTML: object
  12.       },
  13.       mimetype: "text/plain"
  14.   });
  15. </script>
  16.  
  17.  

The example save.php contained a php script that would write the passed variable to a timestamped text file on the server.

So it would write out the debug content from the client to a file on the server that I could access in the comfort of my laptop and review. Made for inspecting the iphones, DOM and JS objects a much happier experience.

A quick follow up, you could always use the firebug on iphone solution that Joe put together. for me, I wanted something then, and hacking debug statements seemed a quick and dirty solution to the immediate need, Joes solution is far more robust, and I’m tempted to try using it on IE for debugging.


About this entry