Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Quick run-through of syncing a Backbone.Collection with a localStorage cache
// Assumes and
var collection = new MyCollection();
var localStore = new Lawnchair({ name: 'my-cache' }, $.noop);
// First load from the cache, then fetch
function initialLoad() {
localStore.all(function(items) {
collection.reset(items, function() {
collection.on('reset', function() {
localStore.nuke(function() {
collection.on('add', function(model) {
model.set('key', model.get('some-non-id-field-usable-as-pkey')); // does nicely in demos;
collection.on('sync', function(model) {;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment