     @save()
     model
 
+  # Update a model by replacing its copy in `this.data`.
   update: (model) ->
     localStorage.setItem @name + "-" + model.id, JSON.stringify(model)
     @records.push model.id.toString()  unless _.include(@records, model.id.toString())
     @save()
     model
 
+  # Retrieve a model from `this.data` by id.
   find: (model) ->
     JSON.parse localStorage.getItem(@name + "-" + model.id)
 
+  # Return the array of all models currently in storage.
   findAll: ->
     _.map @records, ((id) ->
       JSON.parse localStorage.getItem(@name + "-" + id)
     ), this
 
+  # Delete a model from `this.data`, returning it.
   destroy: (model) ->
     localStorage.removeItem @name + "-" + model.id
     @records = _.reject(@records, (record_id) ->

    @save()
    model

Backbone.sync = (method, model, options, error) ->
  if typeof options is "function"
    options =
      success: options
      error: error
  resp = undefined
  store = model.localStorage or model.collection.localStorage
  switch method
    when "read"
      resp = (if model.id then store.find(model) else store.findAll())
    when "create"
      resp = store.create(model)
    when "update"
      resp = store.update(model)
    when "delete"
      resp = store.destroy(model)
  if resp
    options.success resp
  else

###
 * Backbone localStorage Adapter v1.0
 * https://github.com/jeromegn/Backbone.localStorage
 *
 * Date: Sun Aug 14 2011 09:53:55 -0400
###

# A simple module to replace `Backbone.sync` with *localStorage*-based
# persistence. Models are given GUIDS, and saved into a JSON object. Simple
# as that.

# Generate four random hex digits.
S4 = ->
  (((1 + Math.random()) * 0x10000) | 0).toString(16).substring 1

# Generate a pseudo-GUID by concatenating random hexadecimal.
guid = ->
  S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4()

# Our Store is represented by a single JS object in *localStorage*. Create it
# with a meaningful name, like the name you'd give a table.
class window.Store
  constructor: (name) ->
    @name = name
    store = localStorage.getItem(@name)
    @records = (store and store.split(",")) or []

  # Save the current state of the **Store** to *localStorage*.
  save: ->
    localStorage.setItem @name, @records.join(",")

  # Add a model, giving it a (hopefully)-unique GUID, if it doesn't already
  # have an id of it's own.
  create: (model) ->
    model.id = model.attributes.id = guid()  unless model.id
    localStorage.setItem @name + "-" + model.id, JSON.stringify(model)
