Skip to content

Instantly share code, notes, and snippets.

@juliocesar
Created November 19, 2012 00:45
Show Gist options
  • Save juliocesar/4108378 to your computer and use it in GitHub Desktop.
Save juliocesar/4108378 to your computer and use it in GitHub Desktop.
Backbone.Model in localStorage
# Storing a Backbone.Model in localStorage
# ========================================
#
# You can then use localStorage as a collection. For an usage example, see
# https://github.com/juliocesar/factory/blob/master/coffeescripts/factory.coffee#L149
# This largely Just Worked™ for my needs. YMMV!
class MyModel extends Backbone.Model
sync: (method, model, rest...) ->
switch method
when 'create', 'update'
localStorage.setItem @id, JSON.stringify model
when 'delete'
localStorage.removeItem @id
when 'read'
attributes = localStorage.getItem @id
model.attributes = JSON.parse attributes if attributes?
model
@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment