Figure 2 shows the detailed Flash data-binding scheme of the initial incoming table of contacts. If you need instructions on how to apply the data bindings, Mark Rausch does a great job of step-by-step data binding and explaining the update and results Packets in his article, Using the RDBMSResolver to Update a Database with Macromedia Flash Professional 2004.
Open RDBMS-ASP2.fla. On the Content screen, there is an XMLConnector component
named Contacts_xmlcon. The program triggers this component when it first starts.
Since the Contacts_xmlcon results.contacts property is bound out
to the Contacts_ds dataProvider, Contacts_xmlcon automatically sends the table
of data returned from ASP to Contacts_ds.
Figure 2. Flash bindings for initial call to ASP
This is the only time this application triggers Contacts_xmlcon. SaveContacts_xmlcon does all of the following communications with the database; SaveContacts_xmlcon sends only the changes and receives results. In the real world, with thousands of records, you may choose to operate the application differently.
For example, your Contacts_xmlcon could retrieve a subset of the data, avoiding a large initial download. SaveContacts_xmlcon would then save the changes on that data subset, then you could trigger Contacts_xmlcon again for another subset of data to work on.
When the Contacts_ds DataSet component has completed loading from Contacts_xmlcon, it will execute its dataProvider bindings
to the Contacts_grd DataGrid component. The selectedIndex property of Contacts_grd is bound in/out with the Contacts_ds selectedIndex property so that any row selected by the user in the DataGrid will change the pointer in the DataSet to the corresponding record.
The name_txt, email_txt, and phone_txt TextInput components are bound in/out to the corresponding Contacts_ds fields. When a user selects a record in the Contacts_grd DataGrid, the appropriate record's data in Contacts_ds appears in the text boxes for her to edit. You could, alternatively, get rid of the TextInput components and just use the DataGrid to edit the data. Either way works fine. However, I have set the editable property of the DataGrid to false in this example.
Note that all the features you just coded only populate the application with its the initial data; they don't provide for user interaction and data manipulation. Everything you've built up to this point serves its purpose before the user ever interacts with the program! Once the data appears in Contacts_grd, the user can then manipulate the data, using the DataGrid to point to the desired record and the TextInput components to modify the data. As the text fields lose focus when they are changed or edited, you will see the DataGrid update immediately. This indicates that the Contacts_ds DataSet component has updated, because the text fields are only bound to the Contacts_grd indirectly through Contacts_ds.
Figure 3 shows the detailed Flash data-binding scheme for the database save/update cycles. This half of the Flash equation keeps the server and Flash synchronized. This is also the half that made my job much easier. This solved my issues with database updating that I'd experienced when doing this manually in previous versions of Flash.
Figure 3: Flash data binding scheme for database save/update cycles
Assume that you have loaded the data and the user has made some changes to it. When you are ready to save the changes, simply execute the applyUpdates() method of Contacts_ds. In this example, you can press the Save Changes to Database button. The rest is automatic data binding! Here's how it works.
When you execute applyUpdates(), Contacts_ds sends its delta packet, the collection of changes to the data, to Contacts_rs, an RDBMSResolver component. Contacts_rs converts the delta packet into an update packet and sends it to the SaveContacts_xmlcon XMLConnector component. In the example, I have set the kind property of SaveContacts_xmlcon's updatePacket param to AutoTrigger, which causes SaveContacts_xmlcon to automatically trigger an ASP call as soon as the update packet arrives. To see this setting, select the SaveContacts_xmlcon on the Stage, and click the Schema tab in the Component Inspector panel. Select the updatepacket param, you'll find the kind property in the panel below. Macromedia added this AutoTrigger kind to Flash MX 2004 with the 7.0.1 updater. If you don't have it installed, download the 7.0.1 updater now.
When SaveContacts_xmlcon receives a response from ASP, the response contains a results packet that contains the results of all transactions processed from the update packet it received. SaveContacts_xmlcon sends this results packet back to the Contacts_rs through its updateResults property. The Resolver passes the update packet back to the Contacts_ds DataSet as a delta packet.
Contacts_ds then reconciles the delta packet received against the delta packet it previously sent. The DataSet removes all the successfully completed transactions from the delta packet so they don't get sent again. Also, it updates any autonumber or identity fields with their new id values assigned by the database.
The results packet passes back to Flash any errors reported by the server database, like being unable to complete an insert or update for whatever reason. In RDBMS-ASP2.as, you can see the code that shows any errors when the results show up at the DataSet:
DS = new Object();
DS.resolveDelta = function(eventObj) {
var errs:Array = eventObj.data;
for( var i:Number=0; i>errs.length; i++ ) {
ShowMsg( errs[i].getMessage());
}
}
Contacts_ds.addEventListener("resolveDelta",DS);
Because of the addEventListener function, DS.resolveDelta will execute when the DataSet completes the reconciliation process.
When you run the example and save changes to the database, you will see TextArea component labeled packets shows the update packet sent to ASP, and then shows the results packet received back from ASP. This happens rather quickly, so you may not get a good look at the update packet sent. If you change the kind property of SaveContacts_xmlcon.params.data.updatePacket to none, Flash will not send the update packet automatically. You then have time to examine the update packet in the packets window. You can easily copy it to your favorite editor and format it for reading if you like. Then, when you are ready to send the update packet to ASP, just press the Trigger SaveContacts_xmlcon button to continue the cycle.
To change the updatePacket kind property to none:
If you added any new records, you will see the contactid column of the new records update when the cycle completes. ASP has reported back to Contacts_ds the new autonumber values that Microsoft Access assigned when the records were added to the database.