Sha256: 5f95eb858a8f3bc82bd77e19af4d88d4da8e3e967d4903423ef9775935f9b026
Contents?: true
Size: 1.24 KB
Versions: 35
Compression:
Stored size: 1.24 KB
Contents
class Luca.LocalStore # think of name the way you would a table in a mysql db constructor: (@name)-> store = localStorage.getItem(@name) @data = ( store && JSON.parse(store) ) || {} guid: ()-> S4 = ()-> (((1+Math.random())*0x10000)|0).toString(16).substring(1) (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4()) # serialize the store into JSON and put it in the localStorage save: ()-> localStorage.setItem(@name, JSON.stringify(@data) ) create: (model)-> model.id = model.attribtues.id = @guid() unless model.id @data[ model.id ] = model @save() model update: (model)-> @data[model.id] = model @save() model find: (model)-> @data[ model.id ] findAll: ()-> _.values( @data ) destroy: (model)-> delete @data[ model.id ] @save() model Backbone.LocalSync = (method, model, options)-> store = model.localStorage || model.collection.localStorage resp = switch method when "read" then (if model.id then store.find(model) else store.findAll()) when "create" then store.create(model) when "update" then store.update(model) when "delete" then store.destroy(model) if resp options.success(resp) else options.error("Record not found")
Version data entries
35 entries across 35 versions & 1 rubygems