Created
November 19, 2012 00:45
-
-
Save juliocesar/4108378 to your computer and use it in GitHub Desktop.
Backbone.Model in localStorage
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
# 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