LocalStorage
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.
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.
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.
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.
Remove the item from the sotrage. Does nothing if nothing present at the specified key
Clean up everything.
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)
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.
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.
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.
Cobalt is an Open-Source Hybrid Mobile Framework. Read more about it on the home page or in the documentation
- Introduction to Cobalt navigation
- The cobalt.json file
- native navigation bars
- Handling the Android back button
- Introduction to Cobalt messages
- Publish/Subscribe on the Web side
- Publish/Subscribe on Android
- Publish/Subscribe on iOS
- Web Lifecycle messages
- Pull-To-Refresh and Infinite Scroll
- Custom alerts and Toasts
- LocalStorage
- OpenExternalUrl
- PlatformInfos
- Ajax
- Removing the top bar
- Adding Cobalt to an existing project
- Customizing your hybrid views
- Handle multiple Cobalt webviews on the same screen (TODO)