Use Mozilla’s LocalForage For Key-Value Storage In Ionic Framework

A few years ago I wrote an article called Use ngStorage for all Your AngularJS Local Storage Needs, which was intended to be for AngularJS in general. However, I understand many readers were using it in their hybrid Apache Cordova or Ionic Framework applications. There is nothing wrong with this. However, ngStorage is a wrapper for HTML5 local storage which is known to have compatibility issues under certain circumstances. That said, came across localForage, a library by Mozilla, which claims to be a wrapper for local storage, WebSQL, and IndexedDB, offering maximum compatibility.

We’re going to take a look at including localForage in an Ionic Framework Android and iOS application for storing data.

Before getting too far ahead of myself, I actually stumbled upon localForage via a recommendation from Nolan Lawson who was a guest in my podcast, PouchDB and its Usefulness in Development. He said that if you need a local database with no intentions of sync, localForage is a solid choice.

Creating a New Ionic Framework iOS and Android Project

To make this guide easy to understand, we’re going to start a new project and work our way up. This project will be a very simple list of people where we can add new people to the list. This list is saved to localForage and read from as necessary.

To create a new project, execute the following from your Terminal (Linux and Mac) or Command Prompt (Windows):

There are a few things to note in the above commands. First, you’ll notice we are creating an Ionic Framework 1 project, not an Ionic 2 project. We are adding the iOS platform, but if you’re not using a Mac, you cannot build for iOS.

If you’re looking to use localForage in Ionic 2, you should read my other tutorial on the subject.

Including the localForage JavaScript Dependency from Mozilla

To make this library possible in our project, it must be downloaded and included. For this example I’ll be using version 1.4.2 of the library found here. If you’re up for the adventure, try to use the latest version.

After you’ve downloaded a ZIP archive of the release, extract it and look for the dist/localforage.min.js file. This file should be placed in the www/js file of your project.

With the file placed, open your project’s www/index.html file and include the following line:

<script src="js/localforage.min.js"></script>

The above line should be placed right below the include for the cordova.js file. At this point we should be ready to start developing with this new storage form.

Using the localForage Library in Your Ionic Framework Project

We’re going to focus on two particular files. One file will contain all of our AngularJS logic and the other will contain all of our HTML UI code. As previously stated, the project is meant to be very simple.

Starting with the AngularJS code, open the project’s www/js/app.js file and include the following code:

If the key does not exist, a null will be returned which is why we’re using a ternary operator. We want to be working with an array, not a null. To refresh the UI we call $apply() on the application scope.

Now we do have a function called save, but the code that sits in it is mostly taken from the Ionic Framework documentation for $ionicPopup. What we care about is the following:

The $scope.data.firstname and $scope.data.lastname taken from the prompt will be pushed into our people scope and then that scope is saved. Remember, localForage can save anything without first having to serialize or parse.

Now let’s take a quick look at the HTML UI that goes with this logic. Open the project’s www/index.html file and include the following markup:

Above is the same HTML file that we included the localForage library to. Above is just the complete version of it. Notice that we are creating an ion-list and repeating over each item in the array to present on the screen. Just basic UI stuff to match our logic file.

At this point the application should be good to go!

Conclusion

You just saw how to include Mozilla’s localForage JavaScript library in your Ionic Framework mobile application. This library is great because not only is it more compatible than using local storage, but you can save any kind of data to it without parsing or serializing the data first. This guide was for Ionic Framework 1, but if you’re interested in seeing how it’s done with Ionic 2, check out my other tutorial on the topic.

Nic Raboy

Nic Raboy is an advocate of modern web and mobile development technologies. He has experience in Java, JavaScript, Golang and a variety of frameworks such as Angular, NativeScript, and Apache Cordova. Nic writes about his development experiences related to making web and mobile development easier to understand.