A localStorage and sessionStorage adapter for Backbone. It's a drop-in replacement for Backbone.Sync() to handle saving to the browser's browserStorage or sessionStorage database.
Include Backbone.browserStorage after having included Backbone.js:
<script type="text/javascript" src="backbone.js"></script>
<script type="text/javascript" src="backbone.browserStorage.js"></script>
Create your collections like so:
window.SomeCollection = Backbone.Collection.extend({
// For localStorage, use BrowesrStorage.local.
browserStorage: new Backbone.BrowserStorage.session("SomeCollection"), // Unique name within your app.
// ... everything else is normal.
});
Include RequireJS:
<script type="text/javascript" src="lib/require.js"></script>
RequireJS config:
require.config({
paths: {
jquery: "lib/jquery",
underscore: "lib/underscore",
backbone: "lib/backbone",
browserstorage: "lib/backbone.browserStorage"
}
});
Define your collection as a module:
define("SomeCollection", ["browserstorage"], function() {
var SomeCollection = Backbone.Collection.extend({
// For localStorage, use BrowserStorage.local.
browserStorage: new Backbone.BrowserStorage.session("SomeCollection") // Unique name within your app.
});
return SomeCollection;
});
Require your collection:
require(["SomeCollection"], function(SomeCollection) {
// ready to use SomeCollection
});
If you're using browserify.
Install using npm install backbone.browserstorage
, and require the module.
Backbone.BrowserStorage = require("backbone.browserstorage");
This package is a fork of jeromegn's Backbone.localStorage