- What are Components?
- OK, Which Components Do I Get with Flash MX 2004?
- Binding an Array to a Component in Flash MX 2004
- Controlling the Halo Theme for Flash MX 2004 Components
- Building an Application with Components
Binding an Array to a Component in Flash MX 2004
One of the neat tricks in Flash MX 2004 is the enhanced data-binding features available in many of the components. Data binding is the capability to take a set of data, from an array, XML file, or imported dataset, and apply that data to an object to dynamically modify the content of the object.
Because you are keeping the data separated from the component, it means that you can apply the data to additional components. You will see how easy it is to apply the same data source to a ComboBox, Grid, and List component.
If you used FireFly components for Flash MX, you are familiar with databinding. The technique has been worked on and enhanced with Flash MX 2004 and Flash MX 2004 Professional.
So, let's get started. Begin by opening Flash MX 2004 and creating a new Flash document. Save the file as MyStates.fla.
Now, select Frame 1 in the Timeline Panel. Open the Actions panel and add the following array:
// Create Array myStatesArray = new Array(); myStatesArray.push({data:"AL", label:"Alabama"}); myStatesArray.push({data:"AK", label:"Alaska"}); myStatesArray.push({data:"AZ", label:"Arizona"}); myStatesArray.push({data:"AR", label:"Arkansas"}); myStatesArray.push({data:"CA", label:"California"}); myStatesArray.push({data:"CO", label:"Colorado"}); myStatesArray.push({data:"DE", label:"Delaware"}); myStatesArray.push({data:"DC", label:"Dist. Of Columbia"}); myStatesArray.push({data:"FL", label:"Florida"}); myStatesArray.push({data:"GA", label:"Georgia"}); myStatesArray.push({data:"GU", label:"Guam"}); myStatesArray.push({data:"HI", label:"Hawaii"}); myStatesArray.push({data:"ID", label:"Idaho"}); myStatesArray.push({data:"IL", label:"Illinois"}); myStatesArray.push({data:"IN", label:"Indiana"}); myStatesArray.push({data:"IA", label:"Iowa"}); myStatesArray.push({data:"KS", label:"Kansas"}); myStatesArray.push({data:"KY", label:"Kentucky"}); myStatesArray.push({data:"LA", label:"Louisiana"}); myStatesArray.push({data:"ME", label:"Maine"}); myStatesArray.push({data:"MD", label:"Maryland"}); myStatesArray.push({data:"MA", label:"Massachusetts"}); myStatesArray.push({data:"MI", label:"Michigan"}); myStatesArray.push({data:"MN", label:"Minnesota"}); myStatesArray.push({data:"MS", label:"Mississippi"}); myStatesArray.push({data:"MO", label:"Missouri"}); myStatesArray.push({data:"MT", label:"Montana"}); myStatesArray.push({data:"NE", label:"Nebraska"}); myStatesArray.push({data:"NV", label:"Nevada"}); myStatesArray.push({data:"NH", label:"New Hampshire"}); myStatesArray.push({data:"NJ", label:"New Jersey"}); myStatesArray.push({data:"NM", label:"New Mexico"}); myStatesArray.push({data:"NY", label:"New York"}); myStatesArray.push({data:"NC", label:"North Carolina"}); myStatesArray.push({data:"ND", label:"North Dakota"}); myStatesArray.push({data:"OH", label:"Ohio"}); myStatesArray.push({data:"OK", label:"Oklahoma"}); myStatesArray.push({data:"OR", label:"Oregon"}); myStatesArray.push({data:"PA", label:"Pennsylvania"}); myStatesArray.push({data:"RI", label:"Rhode Island"}); myStatesArray.push({data:"SC", label:"South Carolina"}); myStatesArray.push({data:"SD", label:"South Dakota"}); myStatesArray.push({data:"TN", label:"Tennessee"}); myStatesArray.push({data:"TX", label:"Texas"}); myStatesArray.push({data:"UT", label:"Utah"}); myStatesArray.push({data:"VT", label:"Vermont"}); myStatesArray.push({data:"VI", label:"Virgin Islands"}); myStatesArray.push({data:"WA", label:"Washington"}); myStatesArray.push({data:"WV", label:"West Virginia"}); myStatesArray.push({data:"WI", label:"Wisconsin"}); myStatesArray.push({data:"WY", label:"Wyoming"});
The array is called myStatesArray. The array itself is created for the Flash 7 Player. You are using the new "push" method that adds data to the array.
Now you need to add the ComboBox component to the Stage. From the Components panel, select the ComboBox component and drag it onto the stage. Name the ComboBox myStates_cbx.
Want to see how easy it is to bind the data from the array above to the ComboBox? Well, this is it. Open the Actions panel. Select Frame 1 one of the Timeline panel and add the following line after the array:
myStates_cbx.dataProvider = myStatesArray;
By adding the dataProvider method you can bind the myStatesArray to the myStates_cbx. Preview your movie and see that it works.
For each state listed, there is a combination of data and label information. Normally, when you create an array, you can make your list of data information arbitrary. When you are working with the List and ComboBox components, the data and label information are meaningful. The label is what the user sees. The data is additional information that can be used. For instance, here you can see that the two-letter state abbreviation is used as the data information, but the user sees the full state name.
Now try this. Add a List component to the stage. Name the List component myStates_list. After your ActionScript in Frame 1, add the following code:
myStates_list.dataProvider = myStatesArray;
Preview the movie and see that your array is now bound to two components.
If you have Flash MX 2004 Professional you can take it one more step. Add the Grid component to the Stage and name the component myStates_grid.
Go to Frame 1 of the Actions panel and add the following ActionScript below the other code:
myStates_grid.dataProvider = myStatesArray;
Now preview the movie. You see your Grid now lists all of the information from the array. You see that the data and label information comes through because the Grid does not have the special label and data requirement found with the ComboBox and List components.
Binding data to components with Flash MX 2004 is a huge benefit for creating complex Rich Internet Applications. More and more Flash is enabling you to separate your code, data, and graphical layer. The more you can do this, the more flexible your code will become.