A simple module to add a localstorage cache to Backbone.sync.
Objects you have already fetched are retrieved from the local
cache. If the object isn’t cached, it falls back to ‘Backbone.sync’
which does not need to be the default implementation, so it should
be compatible with any custom sync implementations.
To use, extend Backbone.CachedModel instead of Backbone.Model
for your models. Ensure you define a unique cacheName string for
each model or else it won’t use the cache.
This relies on the id attribute of the model. It will not use the
client ID as we want to ensure the object is persisted before we
cache it locally. The model or service (via Backbone.sync) should
assign an id. If there isn’t an id, we always use Backbone.sync
and never the cache.
Status: Alpha
No cache expiry means this isn’t really ready for prime time.
example usage:
var User = Backbone.CachedModel.extend({
cacheName: ‘USERS’,
});