TWiki> Main Web>WebProgrammingStuff? >ClassesInJavascript (20 Mar 2007, MattWalsh)EditAttach
In the search page script I found myself having all these global arrays - one for each thing I needed to store about the sets of results. There are (currently) 6 sets of results - one for each category of things we search. These arrays help me keep track of things like which document element to add to, the text labels that I need to update and so on.

But I got tired of the mess and bugs I got - every time I'd add a new variable I'd have to create a new array, a new place to set the variable and ao on. I found out there is a better way from this article!

// This function gets called when you do 'new Result()'.  You can also
//  have arguments in the function - say if you want to initialize things
//  in the class to something other than null.   
//  
// Each variable (div, label, is_done) here gets 'created' simply by using them.  
//  Javascript is really 'lax' in that way - in most languages you'd have to declare
//  each variable specifically.
   function Result()
   {
      this.div      = null;
      this.label    = null;
      this.is_done  = null;
   }

// Make an array just like normal to store my list of 'Result' entries
   Results = new Array();

// for each new result I want, I do this...
   Results.push(new Result());

// Then to use an element (say, to get the label for 'Music' - element 2)
   Results[which_query].label.nodeValue +=
     ":  " + count + " results found in " + time + "secs";

-- MattWalsh - 20 Mar 2007

Topic revision: r1 - 20 Mar 2007 - 23:17:00 - MattWalsh
 
This site is powered by the TWiki collaboration platformCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback