SharedObject Applications

Efficiency Anyone?
Hasan Otuome
Hasan Otuome is Chief Architect for Marx Media (http://www.marxmedia.net) where he can usually be found developing Rich Internet Applications for the company's clients. He champions creative uses and combinations of Flash, PHP, AJAX and MySQL, among others, for their benefits in user experience improvement.
View all articles by Hasan Otuomevar i:Number;
var tl = this;
var uData:Array = ["var1","var2","var3","var4"];
for (i=0; i<uData.length; i++){
cookie.data[uData[i]] = tl[uData[i]].text;
}
cookie.flush();
So instead of taking 30+ lines of code we accomplish the same thing in 8. Now which one do you think would be easier to troubleshoot if something went wrong? My bet's on the 8-liner. And it also gives us a reusable framework that we can apply to the other "pages" only having to modify the array. This will seriously reduce our develop time and decrease our margin of error. Now that you've got the general idea we can skip over to the end of the app and deal with prepping the data for it's POST-Flash journey.
Ok, the user has made it to the end of the app and has successfully passed all of our validation routines. Now we need to send this data to the client by the required means. Maybe the client wanted the data input into a database or sent via email or both. Whichever was chosen, we'll make use of Flash's LoadVars to handle the task. Remember, we've been collecting pages and pages of data which has more than likely grown quite massive. Therefore, it would be a daunting task to create our LoadVars were it not for our handy looping techniques. Oh yes, we're going to employ the same concept we used on each of the pages to store the data to our SharedObject. Here we're just creating a loop that will build our LoadVars from the data stored in the SharedObject. But before we do that we'll create our LoadVars and messages to display to the user based on what kind of response we receive from our processing script (ASP,JSP,PHP...).
var sender:LoadVars = new LoadVars();
var receiver:LoadVars = new LoadVars();
var successMsg:String = "Data Transmission Successful";
var failureMsg:String = "Data Transmission Failed";
Now we want to create the loop that will build our LoadVars, "sender" with the data from our SharedObject, "cookie" and decide which message to display to the user when our processing script responds to us:
for (i in cookie.data){
sender[i] = cookie.data[i];
sender.sendAndLoad("processor.php",receiver,"POST");
}
receiver.onLoad = function(){
if (this.sent){
msgBox.text = successMsg;
root.KillIt();
} else {
msgBox.text = failureMsg;
}
}
So what we've done is added all the data from our SharedObject to our LoadVars and sent that to our processing script. Our processing script will process all the data and, if successful, will return a variable that our other LoadVars can check for and based on that will display the appropriate message to the user. This approach definitely saved me on a recent project where one of the apps contained over 280 variables! Without looping functionality I'd still be coding right now :) Enjoy!
Hasan
hasan at marxmedia dot net

