The LoadVars class has two events available: onData and onLoad. The onData event TRiggers when a result has been returned from the server, but before the results have been parsed by Flash. The onLoad event TRiggers if you call the load or sendAndLoad method of the LoadVars class, but it triggers after the results have been parsed by Flash. Each event is useful in different circumstances, and they each take different parameters.
It is important to remember that if you are using the onData event in your code, the onLoad event no longer triggers unless you specifically call it from within your code.
The following exercise focuses on handling an event that is triggered when the user changes the value of the List component. You should still be using reviews.fla for this example.
Add an event listener to the List component instance using ActionScript.
Select Frame 1 of the actions layer, and open the Actions panel. Add the following code to the Script pane, below the function that's already there:
This code adds an event listener to your reviews ls List component instance, which triggers when the user clicks a new book title. The addEventListener method takes two parameters: the event that it listens for (in this case, change) and the function to trigger when the event is triggered (in this case, the loadReview function you defined in the previous exercise). There is another option for the addEventListener method, allowing you to specify an object containing a function that is used to handle the event.
Ensure that the document works properly by choosing Control > Test Movie. Then save and publish the document. Return to the authoring environment and save the changes you made to reviews.fla. Publish the FLA file by choosing File > Publish to generate the SWF file. You will load this file into the main Tech Bookstore SWF file in a later lesson.
Test the FLA file by selecting Control > Test Movie. Each time you click a book title in the reviews_ls component on the left of the Stage, Flash calls the loadReview function, which in turn loads that book's review into the TextArea instance. You also should notice that when the contents of the external text files display in the TextArea instance, the external files containing HTML formatting such as bold (using the <b> tag), italics (<i>), and images (<img>) affect the look of the text. If the TextArea component displays the actual HTML source code, including tags, check that the html parameter is set to true in the Component inspector panel. If you look at the contents of the text files, you'll see that the images are actually loading from the Amazon.com servers for this exercise. In reality, you would want to load images from your server locally instead of from the servers at Amazon.com. You could save the image files locally and change the URL in the text files in which you're loading.
The file is generated in the TechBookstore folder. If you want to add more reviews, you can copy the text within an existing review and make changes as necessary. Save the new file into the reviews folder after you have finished editing them. Remember that you'll also need to modify both the data and labels parameters in the Property inspector for the list component and add new values similar to Step 3 of the previous exercise.