Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Add Persistence to Your XML Data Islands-8 : Page 8


advertisement
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:

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.


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.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date