12.07.2015 Views

Pro JavaScript for Web Apps pdf - EBook Free Download

Pro JavaScript for Web Apps pdf - EBook Free Download

Pro JavaScript for Web Apps pdf - EBook Free Download

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 6 STORING DATA IN THE BROWSERYour Details:I have defined a simple three-field <strong>for</strong>m element in this example, which you can see in Figure 6-1.The <strong>for</strong>m captures the user’s name, city, and country and is posted to the /<strong>for</strong>mecho URL at the server,which simply responds with details of the data that was submitted.Figure 6-1. Using local storage with <strong>for</strong>m elementsI have used a view model as an intermediary between the input elements and local storage. Whenthe user enters a value into one of the input elements, the value data binding updates the correspondingobservable data item in the view model. I use the subscribe function to receive notifications of thesechanges and write the update to local storage, like this:$.each(viewModel.personalDetails, function(index, item) {item.value(localStorage[item.name] || "");item.value.subscribe(function(newValue) {localStorage[item.name] = newValue;});});143www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!