Name: Backbone.localStorageSync
Owner: Liip
Description: A caching layer between the client and server
Created: 2012-09-14 12:30:47.0
Updated: 2016-11-07 07:34:12.0
Pushed: 2012-09-20 15:08:08.0
Homepage: null
Size: 132
Language: JavaScript
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
An extension to Backbone providing localStorage caching for sync operations
Backbone.localStorageSync(originalSyncLayer, uniqueNameForCollections, options)
The provided values are the defaults.
{
alwaysUpdate: true, //Fetch the model from original Sync even if it was available in the localStorage
prefix: '' //Used in front of all localStorage keys
}
require(['backbone', 'backbone.localstoragesync'], function(Backbone, localStorageSync) {
var model = new Backbone.Model({
sync: new localStorageSync(Backbone.sync)
});
model.fetch().done(function () {
//Use as normal
});
var collection = new Backbone.Collection({
sync: new localStorageSync(Backbone.sync, 'MyCollection')
});
collection.fetch();
});
Add the script
after Backbone. Then you get a global as Backbone.localStorageSync
and can use it like this:
var model = new Backbone.Model({
sync: new Backbone.localStorageSync(Backbone.sync)
});
model.fetch().done(function () {
//Use as normal
});
var collection = new Backbone.Collection({
sync: new localStorageSync(Backbone.sync, 'MyCollection')
});
collection.fetch();
When you use an AMD loader you need to have those libraries available under the stated name. If you use RequireJS and have trouble check the shim config.
localStorage
and JSON
have to be globaly available otherwise the provided Sync layer will be returned.
Backbone.localStorageSync is Copyright © 2012 Liip AG. It is distributed under the MIT license.
It is inspired by: Backbone.cachingSync Backbone.localStorage