Refractoring the LocalStorage module

We are going to treat the tags as data instead of string. Before that, we will refractor the storage as an individual module inside data object.

 1// Storage Module
 2(function(){
 3  this.storage = {};
 4  this.storage.saveList = function(name, list) {
 5    localStorage.setItem(name, JSON.stringify(list));
 6  }
 7  this.storage.loadList = function(name) {
 8    return JSON.parse(localStorage.getItem(name)) || [];
 9  }
10}).call(data);

Note: we used .call(data) so the this inside that annonymous function refers to the data object.

When we load the books list:

1data.books = data.storage.loadList("books");

When we need to save the book list, we call the following:

1this.storage.saveList("books", this.books);

What’s next? We’re going to take a look at “Planning the tags data structure”.

overlaied image when clicked on thumbnail

Makzan | Mobile web app dev with phonegap | Table of Content