JSON Storage State Source

Provides a simple way of storing JSON objects in local or session storage.

var UserStorage = Marty.createStateSource({
  namespace: 'users',
  type: 'jsonStorage',
  createUser: function (user) {
    this.set(user.id, user)
  },
  getUser: function (id) {
    return this.get(id);
  }
});
class UserStorage extends Marty.JSONStorageStateSource {
  constructor(options) {
    super(options);
    this.namespace = 'users';
  }
  createUser(user) {
    this.set(user.id, user);
  }
  getUser(id) {
    return this.get(id);
  }
}

storage

The web storage to use, can be localStorage or sessionStorage. Default is localStorage.

namespace

An (optional) prefix for keys.

get(key)

Gets the item in the storage with the given key. If the item exists, it will deserialize it.

set(key, obj)

Serializes the object to a JSON string before and then inserts into the storage with the given key.