Skip to content

Instantly share code, notes, and snippets.

@juliocesar
Forked from benschwarz/sync-localStorage.coffee
Last active December 10, 2015 20:48
Show Gist options
  • Save juliocesar/4490507 to your computer and use it in GitHub Desktop.
Save juliocesar/4490507 to your computer and use it in GitHub Desktop.
sync: ( method, collection, options ) ->
return Backbone.sync(method, collection, options) unless 'localStorage' in window
content = localStorage.getItem( @url() )
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 ) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment