Training Guide: Programming in HTML5 with JavaScript and CSS3 Ebook

(Nora) #1

616 CHAPTER 16 Offline web applications


displayCurrentRecord();
};
};

function displayCurrentRecord() {
var contact = currentRecord.contact;
$('#firstName').val(contact.firstName);
$('#lastName').val(contact.lastName);
$('#email').val(contact.email);
$('#phoneNumber').val(contact.phoneNumber);
}

ns.save = function () {
var contact = currentRecord.contact;
contact.firstName = $('#firstName').val();
contact.lastName = $('#lastName').val();
contact.email = $('#email').val();
contact.phoneNumber = $('#phoneNumber').val();

var trans = db.transaction('contacts', 'readwrite');
var contacts = trans.objectStore("contacts");
var request = currentRecord.key != null
? contacts.put(contact, currentRecord.key)
: contacts.add(contact);
request.onsuccess = function (response) {
ns.display();
};
};

})();


  1. In the Solution Explorer window, right-click the default.html file and choose Set As
    Start Page.

  2. Run the website and start adding contacts. Try closing the browser and re-running the
    website to see the persisted contacts.
    The website behaves the same way as in the previous chapter.


Suggested practice exercises


The following additional exercises are designed to give you more opportunities to practice
what you’ve learned and to help you successfully master the lessons presented in this chapter.
■■Exercise 1 Add the ability to delete items from the contact book application.
■■Exercise 2 Convert the contact book application to use Web SQL instead of
IndexedDB. Make sure you use a browser that supports Web SQL, such as Chrome or
Safari.
Free download pdf