Add localstorage stuff

This commit is contained in:
mike-thompson-day8 2015-03-05 00:47:35 +11:00
parent c6c6bfebf1
commit 29e36eb862
1 changed files with 18 additions and 4 deletions

View File

@ -2,6 +2,7 @@
(:require
[schema.core :as s :include-macros true]))
;; -- Schema ------------------------------------------------------------------
;;
;; A Primatic Schema for the contents of `app-db`. At any time we can validate
@ -16,7 +17,7 @@
(def schema
{:todos (s/both (s/pred map?) (s/pred sorted?))
:showing (s/enum :all :done :active)
:blah (s/enum :all :done :active) ;; add this bogus schema item in, then watch the console
:mistake (s/enum :one :two) ;; add this bogus schema item in, then watch the console
})
@ -24,8 +25,7 @@
[db]
(let [res (s/check schema db)]
(if (some? res)
(.error js/console res))
db)) ;; so it can be used in middleare
(.error js/console (str "schema problem: " res)))))
;; -- Default Value ----------------------------------------------------------
@ -37,6 +37,20 @@
;; -- Local Storage ----------------------------------------------------------
;;
;; Set and Get
(def local-storage-key "re-frame-todomvc")
(defn get-local-storage
[]
(let [data (.getItem js/localStorage local-storage-key)]
(when-not (nil? data)
(-> data (js/JSON.parse) (js->clj :keywordize-keys true)))))
(defn set-local-storage!
[db]
(let [data (-> db (clj->js) (js/JSON.stringify))]
(.setItem js/localStorage local-storage-key data)))