Skip to content

Instantly share code, notes, and snippets.

@benschwarz
Last active December 10, 2015 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save benschwarz/4490416 to your computer and use it in GitHub Desktop.
Save benschwarz/4490416 to your computer and use it in GitHub Desktop.
Drop this into a Backbone.Collection to store retrieved JSON in localStorage. The stored content will be used when the user is offLine.
sync: ( method, collection, options ) ->
content = localStorage.getItem( @url() ) if localStorage
if content and not navigator.onLine
options.success JSON.parse content
else
return Backbone.sync( method, collection, options ).done ( response ) =>
localStorage.setItem( @url(), JSON.stringify( response ) ) if localStorage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment