Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

ember-cli-storagekit

A thin layer on top of the HTML5 localStorage and sessionStorage services

ember-addon

readme

ember-cli-storagekit

npm version Build Status Ember Observer Score Code Climate Dependencies Dev Dependencies

A thin layer on top of the HTML5 localStorage and sessionStorage services. It also has a third type of storage called instanceStorage which is used as a failover when localStorage or sessionStorage are not available (such as when a user intentionally disables them).

This addon does not assume your application uses ember data. That being said it would serve well as part of a custom local, session, instance storage adapter.

Installation

ember install ember-cli-storagekit

Basic Usage

Storagekit takes care of JSON.stringify() and JSON.parse() for you, and supports the following methods:

  • setItem
  • getItem
  • removeItem
  • clear
  • length
  • key

Storagekit makes no assumptions about where you would like to make the service available. As such you need to specify your own injections.

initializer

  export function initialize(registry, application) {
    // inject the storage service, which contains each storage type.
    application.inject('route', 'storage', 'storagekit/service:storage');

    // you can also inject each service individually if that is your thing.
    application.inject('route', 'localStorage', 'storagekit/service:local-storage');
    application.inject('route', 'sessionStorage', 'storagekit/service:session-storage');
    application.inject('route', 'instanceStorage', 'storagekit/service:instance-storage');
  }

  export default {
    name: 'inject-storage-service',
    after: 'inject-storagekit',
    initialize: initialize
  };

Local

  // A controller...or route
  // ...snip...
  actions: {
    savePreferences(preferences) {
      // with storage
      this.get('storage.local').setItem('preferences', preferences);

      // with localStorage
      this.get('localStorage').setItem('preferences', preferences);
    }
  }
  // ...snip...

Session

  // A controller...or route
  // ...snip...
  actions: {
    saveSession(session) {
      // with storage
      this.get('storage.session').setItem('session', session);

      // with sessionStorage
      this.get('sessionStorage').setItem('session', session);
    }
  }
  // ...snip...

Instance

  // A controller...or route
  // ...snip...
  actions: {
    storeTemporarily(temporaryData) {
      // with storage
      this.get('storage.instance').setItem('temporaryData', temporaryData);

      // with instanceStorage
      this.get('instanceStorage').setItem('temporaryData', temporaryData);
    }
  }
  // ...snip...

Namespacing

The storagekit env config provides a means for namespacing your application keys. This is how it works:

  // ...snip...
  APP: {
    storagekit: {
      namespace: 'storagekit'
    }
  }
  // ...snip...

Now your incoming keys will be stored like this:

'storagekit:mykey'

IMPORTANT: The namespace defines the storage "world" for all operations, and will give a result based solely on keys that are namespaced within it. Such methods include: #clear, #keys, #length, and #key.

Why is this useful? Because it allows you to change your namespace based on the current environment. For example, you can define a different namespace for your test environment and keep yourself shielded from any setup and teardown methods that modify your browsers local or session storage.

If you do not specify a namespace, then your keys will be stored in the same form that they are received. If you specify a key directly in one of the adapters it will supercede the namespace in the env config.

Generating the Docs

This addon has yuidoc annotations, and uses ember-cli-yuidoc in order to generate and serve them.

Running Tests

  • ember test
  • ember test --server

For more information on using ember-cli, visit http://www.ember-cli.com/.

changelog

ember-cli-storagekit change log

0.4.2

  • #4 [BUGFIX] Fixes issue where the kit was not using the expected instance storage failover. Updates storage support check to happen as an instance initializer. Makes storage support check more robust.

0.4.1

  • [BUGFIX] Updated storage service injection strategy to register instance-storage as fallback instead of applying strategy at injection time. Fixes issue where multiple unique instances of instance storage service could be created when localStorage/sessionStorage are not available.
  • [Improvement] Upped test coverage

0.4.0

  • [UPGRADE] Upgraded to Ember 2.2.0
  • [BUGFIX] Removed namespace keys returned from #keys method
  • [IMPROVEMENT] Breaking Change Removed options argument for global clearing of storage [only applies to storage world now]
  • [IMPROVEMENTt] Removed double pass on keys array in #keys method
  • [CLEANUP] Switched to using es6 expressions where applicable

0.3.2

  • [BUGFIX] Updated initializers to use backwards compatible means of accessing application instance
  • [CLEANUP] Switched to using sinon for stubbing adapter container in unit tests.

0.3.1

  • [BUGFIX] Better storage support check to ios5 browsing mode.

0.3.0

  • [FEATURE] Storage methods respect namespace for all operations including #clear, #keys, #key, and #length

0.2.8

  • [REFACTOR] Refactored adapters to use a common abstract adapter

0.2.7

  • [DOCS] Updated README.md to include code climate, dependecies, devDependencies, and npm package version
  • [DEPENDENCY] Updated dependencies/dev-dependencies to current stable releases

0.2.6

  • [FEATURE] Added #keys method to storage interface.
  • [DOCS] Updated README.md to include examples surrounding storage namespace

0.2.0

  • [CLEANUP] simplified buildNamespace method

  • [FEATURE] made storage namespace configurable

  • [DOCS] Updated yuidocs

  • [DOCS] added ember-cli-yuidoc

  • [DOCS] updated README.md documentation.

  • [DOCS] fixed travis build status