advertisement
Login | Register   
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Download the code for this article
Data islands are an IE-specific technology. Are you able to use IE-specific solutions in your applications? Have you ever used the userData behavior in an application? Do you see the size limitations on userData as a big problem a minor annoyance, or irrelevant to your applications. Join the discussions a Web.dhtml.general and Web.dhtml.scripting to get answers, make comments, or help others with their problems.
Partners & Affiliates
advertisement
advertisement
advertisement
advertisement
Average Rating: 4.7/5 | Rate this item | 15 users have rated this item.
Add Persistence to Your XML Data Islands (cont'd)
Will the Real Data Please Stand Up
The userData persistence method saves the value of the hidden form field; however the data island gets its data from the phone.xml file. You'll need to write another function that loads the saved data into the data island instead. The tricky part is that you want to load the server-based phone.xml file the first time a user loads the page, but on subsequent loads, you want to load the persisted data. The loadData function fulfills that requirment. Add the function to the xmlSave.js file call it from the body element's onLoad event:
advertisement


 function loadData() {
var xmlData=phoneForm.savedData;
xmlData.load("savedXML");
xmlData.value=xmlData.getAttribute
("persistedData");
//if not the first time, check userData first
if((xmlData.value)!="null") {
data.loadXML(xmlData.value);
}
else {
//then check for the file
var dataFile = "c:\\phone.xml";
var forReading = 1;
var ts, s;
var fso = new ActiveXObject
("Scripting.FileSystemObject");
if(fso.fileExists(dataFile)) {
ts = fso.OpenTextFile(dataFile,forReading);
s = ts.readAll();
alert(s);
xmlData.value = s;
data.loadXML(xmlData.value);
}
else {
// If you made it here, this is the first view
// or there's no locally saved data.
// Do nothing, use the server version
return;
}
}
}
The preceding code is almost a reflection of the save() method. First, the code retrieves a reference to the phoneForm's savedData input field. Next, it retrieves the savedXML file from the UserData cache, retrieves the. persistedAttribute key and assigns its value to the form field's value. When the user first loads the page, or if the user has never saved the data, the persistedAttribute key value is "null" and the page populates the data island with the data from the phone.xml file on the server. If the cache contains data, then the call to the loadXML() method loads the data island with the XML stored in the cache. If the cache does not contain data, the application uses the master data from the server.

To test the application, load the page xmlDataDemoWithSave.html into IE. Add a new record, and then click the Save button. Close IE and then reopen it, navigate to the xmlDataDemoWithSave.html file again and move to the last record. The new record you created will reappear.

While this XML data island application is far from complete, you should have a good handle on how to display and manipulate XML data from within IE5. You can see how saving XML-formatted user data provides far more power and flexibility than using cookies. At the very least, you can see that the XML capabilities of IE5 are both flexible and powerful.
Previous Page: Saving Records—A More Elegant Approach  


Tom Duffy , DevX's new JavaScript Pro, is an Associate Professor at Norwalk Community College, Norwalk, CT where he teaches Web Development and Java. Tom is also the Manager of the NCC Ventures Lab, the College's in-house Web design studio.
Page 1: IntroductionPage 5: Deleting Records
Page 2: Laying the Ground WorkPage 6: Saving Records—a Brute Force Approach
Page 3: Updating RecordsPage 7: Saving Records—A More Elegant Approach
Page 4: Adding RecordsPage 8: Will the Real Data Please Stand Up
Please rate this item (5=best)
 1  2  3  4  5
advertisement