Skip to content

Commit

Permalink
follow best practices of re-frame and give an app-db a schema
Browse files Browse the repository at this point in the history
  • Loading branch information
drapanjanas committed Dec 13, 2015
1 parent bef4f05 commit da97f17
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
6 changes: 4 additions & 2 deletions re-natal.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,14 @@ init = (projName) ->

handlersPath = "src/#{projNameUs}/handlers.cljs"
subsPath = "src/#{projNameUs}/subs.cljs"
dbPath = "src/#{projNameUs}/db.cljs"
exec "cp #{resources}cljs/handlers.cljs #{handlersPath}"
exec "cp #{resources}cljs/subs.cljs #{subsPath}"
exec "cp #{resources}cljs/db.cljs #{dbPath}"

edit handlersPath, [[projNameHyphRx, projNameHyph], [projNameRx, projName]]
edit subsPath, [[projNameHyphRx, projNameHyph], [projNameRx, projName]]
edit dbPath, [[projNameHyphRx, projNameHyph], [projNameRx, projName]]

fs.mkdirSync 'src/cljsjs'
exec "echo '(ns cljsjs.react)' > src/cljsjs/react.cljs"
Expand Down Expand Up @@ -283,8 +286,7 @@ init = (projName) ->
exec 'npm i'

fs.unlinkSync '.gitignore'
exec "
node -e
exec "node -e
\"require('react-native/local-cli/cli').init('.', '#{projName}')\"
"

Expand Down
8 changes: 8 additions & 0 deletions resources/cljs/db.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(ns $PROJECT_NAME_HYPHENATED$.db
(:require [schema.core :as s :include-macros true]))

;; schema of app-db
(def schema {:greeting s/Str})

;; initial state of app-db
(def app-db {:greeting "Hello Clojure in iOS and Android!"})
21 changes: 19 additions & 2 deletions resources/cljs/handlers.cljs
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
(ns $PROJECT_NAME_HYPHENATED$.handlers
(:require
[re-frame.core :refer [register-handler path trim-v after dispatch]]))
[re-frame.core :refer [register-handler after]]
[schema.core :as s :include-macros true]
[$PROJECT_NAME_HYPHENATED$.db :refer [app-db schema]]))

(def app-db {:greeting "Hello Clojure in iOS and Android!"})
;; -- 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 --------------------------------------------------------------

(register-handler
:initialize-db
validate-schema-mw
(fn [_ _]
app-db))

(register-handler
:set-greeting
validate-schema-mw
(fn [db [_ value]]
(assoc db :greeting value)))
3 changes: 2 additions & 1 deletion resources/project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
:dependencies [[org.clojure/clojure "1.7.0"]
[org.clojure/clojurescript "1.7.170"]
[reagent "0.5.1" :exclusions [cljsjs/react]]
[re-frame "0.5.0"]]
[re-frame "0.6.0"]
[prismatic/schema "1.0.4"]]
:plugins [[lein-cljsbuild "1.1.1"]
[lein-figwheel "0.5.0-2"]]
:clean-targets ["target/" "index.ios.js" "index.android.js"]
Expand Down

0 comments on commit da97f17

Please sign in to comment.