From 29e36eb862d0b261f3dc25cd164ca11e06d3a2b3 Mon Sep 17 00:00:00 2001 From: mike-thompson-day8 Date: Thu, 5 Mar 2015 00:47:35 +1100 Subject: [PATCH] Add localstorage stuff --- examples/todomvc/src/todomvc/db.cljs | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/examples/todomvc/src/todomvc/db.cljs b/examples/todomvc/src/todomvc/db.cljs index 1edde92..4cb9287 100644 --- a/examples/todomvc/src/todomvc/db.cljs +++ b/examples/todomvc/src/todomvc/db.cljs @@ -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))) \ No newline at end of file