Practice exercises CHAPTER 15 573
contactsNamespace.initialize();
});
(function () {
this.contactsNamespace = this.contactsNamespace || {};
var ns = this.contactsNamespace;
var currentRecord;
ns.initialize = function () {
};
})();
- To add code to the ContactsLibrary.js file, add a retrieveFromStorage method that pulls
the contact list from localStorage.
The contact list will be stored as an array of JSON Contact objects, but remember that
localStorage supports only storage of string values, so you must parse the result as an
array. You must also handle occasions when the contacts might not yet exist in storage.
Your code should look like the following.
(function () {
this.contactsNamespace = this.contactsNamespace || {};
var ns = this.contactsNamespace;
var currentRecord;
ns.initialize = function () {
};
function retrieveFromStorage() {
var contactsJSON = localStorage.getItem('contacts');
return contactsJSON? JSON.parse(contactsJSON) : [];
}
})();
- Populate the grid by iterating through the results array and creating a row for each
record. Within each row, include three cells: the email address, the full name, and
an edit link that will include a data-key attribute to hold the contact’s index within
the array. - If the array is empty, display a “No records available” message. Add a publicly available
display method that pulls items from storage and passes them to a private bindToGrid
method.
Your code should look like the following.
ns.display = function () {
var results = retrieveFromStorage();
bindToGrid(results);
};
function bindToGrid(results) {