ColdFusion, WDDX, and Flash (Page 7 of 8)
THE FLASH SIDE (Continued)
Let's skip a couple of lines of code (don't worry we'll come back to them) and look at the line converter = new WDDX();. This line of code creates an instance of the WDDX class defined in wddx.as. We create a new XML object on the next line of code. The XML() class is inherent to Flash and not something defined in the wddx.as file. As I mentioned earlier, any whitespace (newlines, carriage returns, tabs, spaces etc.) can foul up our successful parsing of the XML we have read in. Because of this, and as a safety precaution, we tell our XML object (myXML) to ignore any whitespace found in our WDDX packet. myXML.onLoad = loaded; tells Flash to call the "loaded" function as soon as our XML is read from the ColdFusion output. The last line is a biggie. Simple as it is, this is the line of code that tells Flash to call our ColdFusion template retrieving our database information. The CF template writes the WDDX packet to the screen. The packet is then copied into our XML object "myXML". Finally, our loaded function is executed so let's take a look at what it does.
The first line of the function if (valid) says: "If the myXML object has been loaded (via the onLoad event) then execute the following code." _root.queryResults = _root.converter.deserialize(this) deserializes our WDDX packet into an array. The code that follows is what pulls the FIRST employee out of our queryResults array. There is one line of code for each variable assignment using the getField function defined in wddx.as. The string to the left of the equals operator is the variable we are using to store our database information. (Hence why we named our dynamic text fields the same as we are naming these variables) The string to the right of the equals sign (in the case of the EmployeeID) says: "Go to the queryResults array (where our data now resides) and get the FIRST (donated by the 0) value of the variable EmployeeID." This stores the first EmployeeID returned from our ColdFusion query in the _root.EmployeeID variable. The rest of the lines perform the same task with the other column names. As an additional example: say you wanted to grab the last name of the fourth employee. You would write the following code: _root.LastName.text = _root.queryResults.getField(3, "LastName");. Pretty simple.
Writing more ActionScript code and using more of the functions in the wddx.as file you could create a movie in which all the employees were displayed one per page. Next and previous buttons would allow the user to navigate through all the employees in the database. The possibilities are really limitless! Wow, we're done. So what does the Flash movie look like when published?PREVIOUS | NEXT