Created
May 23, 2012 02:10
-
-
Save ykhs/2772849 to your computer and use it in GitHub Desktop.
I referred to this entry. (http://d.hatena.ne.jp/griefworker/20111117/backbone_sync)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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