Integrate storage

Add remoteStorage to your app

Adding remoteStorage to your app

First, download the latest remoteStorage.js release linked above, and add it to your page:

<script type="text/javascript" src="remoteStorage.min.js"></script>

Create a module

remoteStorage.js is based on modules, so if you're not using an existing one, define a new reusable module in order to access and manipulate data from your app.

The defineModule method expects a module name and a builder function. The builder will be called immediately, with two arguments, which are both instances of BaseClient:

RemoteStorage.defineModule('tasks', function(privateClient, publicClient) {

  // Define a common data type using JSON Schema
  privateClient.declareType('task', {
    "description": "a task",
    "type": "object",
    "properties": {
      "id": {
        "type": "string",
        "format": "id"
      },
      "title": {
        "type": "string"
      },
      "completed": {
        "type": "boolean"
      }
    }
  });

  return {
    exports: {
      // Add functions for retrieving and manipulating data using
      // methods provided by BaseClient
      addTask: function (title) {
        var id = new Date().getTime().toString();
        return privateClient.storeObject('task', id, {
          id: id,
          title: title,
          completed: false
        });
      }
      // define more functions...
    }
  };
});

Using the module in your app

First, you need to claim access for the module:

remoteStorage.access.claim('tasks', 'rw');

Then you can include the remoteStorage widget, which allows the user to connect to their storage server:

remoteStorage.displayWidget();

Now create your first task! You can start storing data immediately, even before the user has connected their storage account:

remoteStorage.tasks.addTask('Conquer the world').then(
  function(){
    // object was stored
  },
  function(error) {
    // something went wrong
  }
);

That's it. Now add some more functions to the module, or check out the existing modules. Whenever you create a new module, you should share it with the community, so that apps stay interoperable and everybody's happy.