Event-Driven Code


Looking back at our general XML display code, we see that the problems with our current design are glaring. It is procedural in all the worst ways. It executes sequentially rather than on events, and it elaborates itself through three handcoded levels of hierarchy rather than through infinite depths. XML does not follow such a limit. So we go again into the code and rebuild it from the inside out.

Return to our xmltrace code. We first restructure it as event-driven code, paralleling the work we did at the end of the last chapter. All of the xmltrace object is now a single frame (so is the base movie), and the active code is all packaged into the onLoad handler. In part, that function looks like this:

ActionScript
 xml.onLoad = function ( ok ) { if( !ok ) {  trace ("failure"); return;} trace( this.toString()); l=this.childNodes.length; trace( "----------"+ l +" nodes-------------"); for( var i=0; i<l; i++){   t=this.childNodes[i].nodeType;   trace( "type "+(t==1? "Element" : t==3? "Text":("unknown: "+Number(t))));   trace( "name  "+this.childNodes[i].nodeName);   trace( "value "+this.childNodes[i].nodeValue);   for (name in this.childNodes[i].attributes)     trace( "attribute " +name+":  "+ this.childNodes[i].attributes[name]);     if( ll=this.childNodes[i].childNodes.length )       trace( " --------"+ ll +" subnodes-------------");     for( var ii=0; ii<ll; ii++){       t=this.childNodes[i].childNodes[ii].nodeType;       trace( " type "+(t==1? "Element" : t==3? "Text":("unknown:         "+Number(t))));       trace( " name "+this.childNodes[i].childNodes[ii].nodeName);       trace( " value "+this.childNodes[i].childNodes[ii].nodeValue);       for (name in this.childNodes[i].childNodes[ii].attributes)         trace( "attribute" +name+":"+   this.childNodes[i].childNodes[ii].attributes[name]);         if(lll=this.childNodes[i].childNodes[ii].childNodes.length)           trace( "   ------"+ llll +"subsubnodes-------------");         for( var iii=0; iii<lll; iii++){           trace( "    name   "+this.childNodes[i].childNodes[ii].childNodes[iii].nodeName);           trace( "    type   "+this.childNodes[i].childNodes[ii].childNodes[iii].nodeType);         }      }     }   } 

This code turns our previous code into an event handler. It rewrites the standard event handler. The first two lines set up the event handler and then make sure that no error exists in the event handler call. The rest of the code has all been seen before, with the exception of this , which now appears in a great many places in the code. xml was changed to this because the code no longer is external to an object but rather is a function of the object. Hence this refers to xml, the object that owns this code.



Flash and XML[c] A Developer[ap]s Guide
Flash and XML[c] A Developer[ap]s Guide
ISBN: 201729202
EAN: N/A
Year: 2005
Pages: 160

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net