A plugin for backbone models to save & load data using localstorage as well
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.
Backbone.Safe is now in version 3
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();
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.
this version define 'Safe' as a first level backbone plugin. You can define safe as a key in a Backbone's Object props.
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 );
Download: Backbone.Safe
By Oren Farhi