Skip to content

Instantly share code, notes, and snippets.

@ykhs
Created May 23, 2012 02:10
Show Gist options
  • Save ykhs/2772849 to your computer and use it in GitHub Desktop.
Save ykhs/2772849 to your computer and use it in GitHub Desktop.
var collection = Backbone.Collection.extend({
originalSync: Backbone.sync,
sync: function(method, model, options) {
var originalSuccess, cache;
if (method !== 'read') {
this.originalSync(method, model, options);
return;
}
originalSuccess = options.success;
cache = localStrage.getItem(model.url);
if (cache) {
originalSuccess(JSON.parse(cache));
return;
}
options.success = function(collection) {
localStorage.setItem(model.url, JSON.stringify(collection));
originalSuccess(collection);
};
this.originalSync(method, model, options);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment