re-natal/resources/cljs-reagent/handlers.cljs

32 lines
907 B
Plaintext
Raw Normal View History

2015-11-20 19:22:02 +00:00
(ns $PROJECT_NAME_HYPHENATED$.handlers
(:require
[re-frame.core :refer [register-handler after]]
[schema.core :as s :include-macros true]
[$PROJECT_NAME_HYPHENATED$.db :refer [app-db schema]]))
2015-11-20 19:22:02 +00:00
;; -- Middleware ------------------------------------------------------------
;;
;; See https://github.com/Day8/re-frame/wiki/Using-Handler-Middleware
;;
(defn check-and-throw
"throw an exception if db doesn't match the schema."
[a-schema db]
(if-let [problems (s/check a-schema db)]
(throw (js/Error. (str "schema check failed: " problems)))))
(def validate-schema-mw
(after (partial check-and-throw schema)))
;; -- Handlers --------------------------------------------------------------
2015-11-20 19:22:02 +00:00
(register-handler
:initialize-db
validate-schema-mw
2015-11-20 19:22:02 +00:00
(fn [_ _]
app-db))
(register-handler
:set-greeting
validate-schema-mw
2015-11-20 19:22:02 +00:00
(fn [db [_ value]]
(assoc db :greeting value)))