Skip to content

Instantly share code, notes, and snippets.

@spiechu
Created April 15, 2012 18:07
Show Gist options
  • Save spiechu/2394174 to your computer and use it in GitHub Desktop.
Save spiechu/2394174 to your computer and use it in GitHub Desktop.
Just playing with CoffeeScript, jQuery and localStorage
$ ->
retrieveStoredFormFields = ->
# przejezdzam po wszystkich elementach formularza
# przeznaczonych do zapamietania
$('form .storeMe').each ->
# @ jest aliasem this
fieldValue = localStorage.getItem $(@).attr('id')
# jesli jest cos w localStorage to przypisuje elementowi formularza
if fieldValue then $(@).val fieldValue
setupFormFields = ->
$('form .storeMe').each ->
# 'przyklejam sie' do kazdego wcisnietego klawisza
$(@).keyup ->
# zapisuje do localStorage
localStorage.setItem $(@).attr('id'), $(@).val()
showStoredFieldForms = ->
# tworze tablice zlozona z ciagu pole - wartosc
storedValues = ("pole: #{key} - wartość: #{value}" for key, value of localStorage)
storedString = ''
# calosc sklejam w jeden ciag
storedString += "#{value}, " for value in storedValues
if storedString isnt '' then alert "Wartości przechowywane w localStorage: #{storedString}"
else alert 'Brak przechowywanych wartości w localStorage'
# testuje czy przegladarka obsluguje localStorage
# zwroc uwage, ze zmienna w Modernizr to localstorage, a nie localStorage
if Modernizr.localstorage
showStoredFieldForms()
retrieveStoredFormFields()
setupFormFields()
$('input#clear').click (event) ->
# zapobiegam wyslaniu formularza
event.preventDefault()
localStorage.clear()
# bajeranckie przejscie
$('p#message').fadeOut 500, ->
$(@).text('Wyczyszczono localStorage, teraz naciśnij "Wyślij"').fadeIn 500
else alert 'Przegladarka nie obsluguje mechanizmu localStorage'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment