More complex keys

Storing object in local storage makes it more useful. But that's not enough.

We can further enhnace this feature by making creative use on the key.

How about we store object's ID inside the key?

Imagine now we want to build a JavaScript based todo list.

Let's take a look at the following pseudo local storage.

1'lists' = '[1,2,3]'
2'lists:1' = '{name: "list name", order: 1}'
3'lists:2' = '{name: "list name", order: 2}'
4'lists:3' = '{name: "list name", order: 3}'
5'lists:1:todos' = '["task name 1", "task name 2"]'

Note that the colon (:) is just a notation. The key accepts string so any serialization works here. For example, you may use:


I use colon mark because it is more readable.

What’s next? We’re going to take a look at “Complex keys – Hands on exercise”.

overlaied image when clicked on thumbnail

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