persistence, but it's too magic

This commit is contained in:
Corey Johnson
2011-09-15 16:53:18 -07:00
parent 8011e2fc7c
commit 7347f4c8a4
2 changed files with 60 additions and 41 deletions

View File

@@ -10,7 +10,14 @@ class Pane
keymap: {}
persistantProperties: {}
editableProperties: {}
constructor: (options={}) ->
@createPersistentProperty(k, v) for k, v of @persistantProperties
@createPersistentProperty(k, v) for k, v of @editableProperties
for option, value of options
@[option] = value
@@ -24,10 +31,38 @@ class Pane
@[method]()
else
console.error "keymap: no '#{method}' method found"
@initialize options
# Override in your subclass
initialize: ->
createPersistentProperty: (property, defaultValue) ->
storedPropertyName = "__" + property + "__"
Object.defineProperty @, property,
get: ->
key = @persistentanceNamespace() + property
if @[storedPropertyName]
# Cool, just chill for awhile
else if localStorage[key]
try
@[storedPropertyName] = JSON.parse(localStorage[key] ? "null")
catch error
@[storedPropertyName] = defaultValue
error.message += "\n#{key}: #{JSON.stringify localStorage[key]}"
console.log(error)
else
@[storedPropertyName] = defaultValue
return @[storedPropertyName]
set: (value) ->
key = @persistentanceNamespace() + property
try
@[storedPropertyName] = value
localStorage[key] = JSON.stringify value
catch error
error.message += "\n value = #{JSON.stringify value}"
console.log(error)
toggle: ->
if @showing
@@ -39,3 +74,8 @@ class Pane
activeWindow.addPane this
@showing = not @showing
# Override these in your subclass
initialize: ->
persistentanceNamespace: -> @.constructor.name