Skip to content
Guillaume Gendre edited this page Jun 7, 2019 · 15 revisions

LocalStorage is a persistent storage available from JavaScript. It is specific to a website but shared across all tabs you may have opened on this site.

In a Cobalt application, you may need to manipulate data across the different pages you show. For this purpose, LocalStorage is the easiest way to go.

For convenience, we created a small LocalStorage helper, to store anything and retrieve exactly the same things when you need them.

Cobalt LocalStorage helper

LocalStorage is a string-only storage by default. We have created a LocalStorage helper in Cobalt to help you store JSON objects, dates, numbers or anything in LocalStorage.

cobalt.storage.set(key, value)

Use this to store whatever you want in LocalStorage.

  • key: the key or id of the item you want to store (string)

  • value: The value. It can be anything (except a function) : a string, a number, a date, a JSON object, and Array, or nothing (undefined, null, ""), whatever.

cobalt.storage.get(key)

Get the value of the item from the localStorage.

  • key: the key of the item you want to retrieve (string)

Return undefined if nothing at the specified key.

Warning : We also fixed things here : if you store undefined you will get a undefined. If you store a null you will get a null. That's not the case with basic W3C LocalStorage. You will get the things in the same type as they were stored.

cobalt.storage.remove(key)

Remove the item from the sotrage. Does nothing if nothing present at the specified key

cobalt.storage.clear()

Clean up everything.

examples

 cobalt.storage.set('town','Lannion');
 cobalt.storage.get('town');
 //returns 'Lannion'

 cobalt.storage.set('age',12);
 cobalt.storage.get('age');
 //returns 12 (number)

 cobalt.storage.set('user',{name:'toto',age:6});
 cobalt.storage.get('user');
 //returns {name:'toto',age:6} (object)

A note about dates

You can store a Date object with setItem and expect it to be a Date when using getItem

 cobalt.storage.set('now',new Date());
 cobalt.storage.get('now');
 //returns Thu Aug 06 2015 09:41:45 GMT+0200 (CEST) (Date)

But you won't have Date objects if they are somewhere in a JSON object.

 cobalt.storage.set('complex',{ age : 12, now : new Date()});
 cobalt.storage.get('complex');
 //returns {age: 12, now: "2015-08-06T07:43:08.139Z"} (object)

You will have to transform it to a Date object yourself using new Date(complex.now) for example.

You can still use window.localStorage

On every platform, except Android, LocalStorage is shared across the WebViews. On Android it was a bit messy. An item created from the WebView A was available from the WebView B. But when it was updated from the WebView B, it wasn't on the WebView A.

We have fixed this by using an Android database and redirecting all requests to LocalStorage to the native one. So, as soon as Cobalt is ready you can use window.localStorage and it will use our fixed one for Android.

What about sessionStorage ?

Well, it's not available right now. It may work for iOS but probably not on Android.

Meanwhile, you can delete all the keys you have used or make a clear at application start-up with the "cobalt:onAppStarted" message.

Clone this wiki locally