Skip to content

Instantly share code, notes, and snippets.

@ykhs
Created May 23, 2012 02:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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