Set data to the local storage of a device or browser

Save small amounts of data to a users device or browser while offline

It’s possible to save data locally on your device (if you’re using a mobile or a tablet) or in the browser (if you’re using a webapp on a desktop). The space available isn’t huge but it’s enough to save things like a survey form entry while you’re not connected to the internet.

To set data into the local storage of your device or browser you can use:

JavaScript

Fliplet.App.Storage.set('userData', { name: 'Nick' });

The above code gives the data the name of userData and saves the value of { name: 'Nick' }. The value is an object so it’s possible to save multiple values and data types (ie, words, numbers, arrays). For example you can save data like this:

JavaScript

Fliplet.App.Storage.set('userData', { 
  name: 'Nick',
  age: 32,
  faveMovies: [ 'Frozen', 'Frozen 2', 'The Lion King' ] 
});

If you want to do something after saving the data you can use the following code. You can do different things depending on whether the data is saved successfully. If the data is saved successfully you can run code in the first function. However, if the data fails to save for any reason you can run some code using the catch function.

JavaScript


// Function to save data
function saveUserData(data) {
  return Fliplet.App.Storage.set('userData', data);
}

saveUserData({ name: 'Nick' })
  .then(function () {
    // Run after the data has been saved successfully
    Fliplet.UI.Toast({
      type: 'regular',
      title: 'Data saved',
      message: 'Your data was saved successfully.'
    });
  });