« Flash Cookies :: Yes my friend, feel the love | Main | localToLocal :: coordinate conversions made easy »

LoadVars Object :: how it works, and then some

Headerbutton_flash_actionscript

The LoadVars Object was introduced with Flash MX in 2002.  Now, instead of using loadVariables or loadVariablesNum, which forced you to send all variable information in your timeline, you could use the LoadVars object and send only what was neccessary.  On top of that, you don't have to define an onData event for a movie clip or timeline.  You simply define a function for the onLoad event of the LoadVars object and bingo, you're done.  Also, the onLoad event carries with it a 'success' flag (onLoad = function(success)).  This flag is a huge improvement over the Flash 5 method of loading variable information.  If success is false, this means that your server or server-side script has returned an error.  This is very helpful in debugging applications and narrowing down where your errors are coming from.  And, like the onData event for a movie clip receiving data, the onLoad only fires when the data is finished returning.

NOTE:  There are compatibility issues with certain browsers and Operating systems.  The issues surrounds using "POST" with certain browsers.  The most prone to have problems is Netscape Navigator on either Windows or Mac platforms.  Certain IE versions on the Mac have the same issue.  Use "GET" if you're experiencing problems.  I don't have specific browser version numbers, but at Zing.com, we don't support Netscape at all, and any IE browser that is 5.5 or higher has tested out just fine.

This example shows you one way to return data and deal with multiple "rows" in a querystring format.  Since we're not dealing with Remoting here, we can't receive a nice array of information, so, we have to return it in a var/val pair like the example below.

we start and end with "&", this is to avoid any confusion as to when the value pair starts and ends:
&Num=3&txtNam0=John&txtName1=Erik&txtName2=Jeff&

If you're using MX to load data from an ASP/PHP/JSP page, here's how you *might* go about it.

// Create a new loadVars object
lv = new LoadVars();

// let's define the onLoad function right here.  you *can* define a separate function
// if you wish, and have the onLoad event call that instead.
// It would look like: lv.onLoad = mySeparateFunction;
// NOTE:  you just need the name, don't include () - this isn't a 'call' its just setting
// a parameter for the returning data

lv.onLoad = function(success){
     if(success){
          // the trip to and back from the server was a success
          if(lv.Num > 0){
               // there are vars to process
               for(x=0;x<lv.Num;x++);
                    // next line assumes that we have textboxes
                    // with instance names of "TextBox1", "TextBox1" etc
                    // and that the returned vars are "text1", "text2" etc
                    _root["TextBox" + x].Text = lv["text" + x];
               }
          }else{
               // no data to return/recordset empty
          }
     }else{
          // the trip to and back from the server was NOT a success
     }
}

// Now, add some variables to send to the server
lv.firstName = "John";
lv.lastName = "Grden";

// sendAndLoad(pageToCall, object to return the data to, [how to send the variables. POST is default

lv.sendAndLoad("myPage.asp",lv, "GET");

NOTE:
If you test in the Flash IDE, you'll have to:
  * hardcode the pages entire address
  * change the receiving pages request to look
     for the variables in the querystring since the Flash
     IDE doesn't send variables using POST - even if you specify
     to do so.  This is because Flash uses the browser to send the
     neccessary header for posting data back to the server.

The beautiful part is that the onLoad function doesn't fire until all data is returned.  So, if you're needing to wait for the data before continuing on in the timeline, just stop() here and in the success part of the 'if' statement, do a play() or goto.  Also, the success flag let's you know if there was a problem in talking to the server.

Now, in this example, I returned a "Num" variable to see how many records are being returned.  You can just as easily do an "if" statement to see if the variable is defined:
for(x=0;x<lv.Num;x++)
{
   if(lv["text" + x] != "undefined")
   {
      // do what you need to
   }
}

One last thing! LoadVars.send and framesets
There is an issue/bug with Flash player 6 whereby if you have a Flash movie embedded in an HTML file that is located in a frameset, you can only send variables using "GET".  If you try to use "POST" it will not work.  The best practice is to always use sendAndLoad.  For more information, check out Muzakdeezign.com.

Any questions?  Send me an email: [email protected]

December 20, 2004 in Flash ActionScript | Permalink

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/t/trackback/1569086

Listed below are links to weblogs that reference LoadVars Object :: how it works, and then some:

Comments

Post a comment






 
deck patio furniture | cat furniture | bernhardt furniture | harlem furniture | bush furniture | gallery furniture | cat furniture | furniture rental | used furniture | rustic furniture