The flow of data between Flash, JScript, and a browser's cookie database is probably the most important concept in terms of understanding not just FlashTone, but client-side data storage in general. Because it is so crucial, I am going to take a moment to outline data flow outside of the context of FlashTone where it is free from distractions.
After I have described the concepts at a high level, we will zoom in and see them in action as we explore the FlashTone application.
Using JScript to Retrieve Cookie Data
Using HTTP headers is one way to get and set cookies; however, because our Flash application is running locally without a server component, this technique does us no good. Fortunately, JScript allows us to make and retrieve entries in the browser's cookie database right from the client without even having to communicate with a server or even understand anything about HTTP.
Because one of the most convenient times to pass data into a Flash movie is at the time the movie loads (more on this to come), the flow of data must start with the loading of the HTML page in which your Flash movie is embedded. As the page loads, therefore, we will use JScript to access the browser's cookie database, find the exact cookie we are interested in (in this case, the cookie containing the user's 10 most frequently dialed phone numbers), and save that data to a JScript variable. Step one accomplished. The next challenge is getting that data from a JScript variable into Flash.
Using JScript to Pass Data to Flash
It seems we have a problem. We want to pass data into our Flash movie at the time the movie loads; however, the data is contained in a JScript variable and the movie is loaded from the HTML <object> tag. To get the data from JScript to HTML and finally into our Flash movie, therefore, we will apply the technique of actually using JScript to construct the HTML tags which load the movie, slipping our data in at just the right moment where it will get passed into the Flash movie and become available as an ActionScript variable.
Flash Passing Data Back to JScript
Now the user's 10 most frequently dialed phone numbers have been loaded into the Flash movie and can be accessed easily. But what if the user wants to change one of the phone numbers? Or what if he only has five slots taken and wants to add a sixth number? Not only does the new number have to be stored in Flash, but also it somehow has to make its way back into the browser's cookie database so that it will be there the next time the movie is loaded. Because JScript has the ability to read and write cookies and Flash does not, we will see how Flash can pass data to Jscript functions.
JScript Saving Data as Cookies
Now that the new phone number has been passed from the Flash movie to JScript, it is time to complete the data flow cycle. Figure 8.1 shows how JScript, in addition to reading cookies, can write cookies to the browser's cookie database.
Figure 8.1. The JScript data flow cycle.