HTML5 persistent storage in bada web applications

Gabor Paller's picture

Persistent storage is an important aspect of application design and similarly to Sfonge's bada C++ course, I will introduce it by implementing a small feature in the first iteration of our "interesting words" application. This feature is that the application remembers the last selected word and when restarted next time, it displays that word.

In the C++ version this was implemented with the AppRegistry facility. In the web application model there is a cool new way of providing persistent storage using HTML5 features. HTML5 is an emerging web standard and bada 2.0 supports it. In particular, the persistent storage features of HTML5 are also supported which are already quite stable in the standard and are implemented in popular browsers like Chrome and Mozilla. If you are not familiar with HTML5, this presentation is the best to start with.

The example project is attached at the end of the post.

After all this introduction, the actual code is ridiculously simple. HTML5 as it is now offers three offline storage mechanism: WebStorage, Web SQL database and Indexed Database. WebStorage is the simplest and that's what we are going to use. WebStorage allows the page to store key-value pairs persistently. So when the application is opened, we look into this storage whether the last word index has been stored.

var storedIndex = window.localStorage.getItem( "index");
if( storedIndex != null )
   this.index = storedIndex;

and when the index is updated, we also store it in the persistent storage.

window.localStorage.setItem( "index",this.index);

One advice if you want to see in the emulator that the persistent storage really works. By default, if you select "Run as/bada Emulator web application", the developer IDE deploys the application again deleting any persistent data stored during the application's previous execution. Click Run as/Run configurations and untick both the remove ... data files and overwrite ... data files check boxes. Then if you start the application again with the "Run as" menu item, you will be able to see how the previously saved index is retrieved from the persistent storage.

hari patil's picture

Great information , it will really helps and guides many , i m very much impressed with this post.Thanks for the information.