Backbone.Safe

A plugin for backbone models to save & load data using localstorage as well

View the Project on GitHub orizens/Backbone.Safe

Backbone.Safe - local storage plugin

Backbone.Safe is a plugin for Backbone.js which stores a model's or a collection's json data to the local storage on set operations, regardless server side existance. Run the latest Unit Tests to inspect the various features.

Latest Features

Backbone.Safe is now in version 3

20/11/2013

the localStorage data can be loaded via the 'fetch' method with this configuration, i.e: fetch({ from: 'safe' }); is the same as this.safe.reload();

3/1/2013

The 'reload' attribute is active now. If set to 'false', data won't be loaded from localstorage. It can be loaded later by calling the 'reload' method.

2012

this version define 'Safe' as a first level backbone plugin. You can define safe as a key in a Backbone's Object props.

Usage

You can still create Backbone.Safe on demand as such:

var HistoryPlaylist = Backbone.Collection.extend({
    model: someModel,

    initialize: function() {
        Backbone.Safe.create('historyPlaylist', this);
    },

    queue: function(youtubeJSON) {
        this.add(youtubeJSON);
    }
});
var myPlaylist = new HistoryPlaylist();

console.log( myPlaylist.safe );

Useful Information

  1. Currently supports only one level of models/collections.
  2. For Model: listens to a 'change' events and stores the data.
  3. For Collection: listens to 'add', 'reset' events and stores data.
  4. The 'create' function creates a new instance of Backbone.Safe in the 'safe' property
  5. Currently, the 'options' property has only one options - 'reload': true (as a default) - it reloads the data that is stored in the cache after a refresh or on recreation.

Download: Backbone.Safe

By Oren Farhi