Skip to content

Instantly share code, notes, and snippets.

@greyarch
Last active February 7, 2017 11:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greyarch/a8147ca45e91874dc422a60428f3890b to your computer and use it in GitHub Desktop.
Save greyarch/a8147ca45e91874dc422a60428f3890b to your computer and use it in GitHub Desktop.
cljs reagent firebase
(ns try-clojure.app
(:require
[try-clojure.db :as db]
[try-clojure.ui :as ui]
[reagent.core :as r]))
(enable-console-print!)
(defonce root (db/connect "https://try-clojure.firebaseio.com/counter"))
(defonce state (db/sync (atom {}) root []))
;
(defn counter-card
[title path]
(ui/counter-card title
(deref (r/cursor state path))
#(db/update! root path inc)
#(db/update! root path dec)))
(defn app []
[counter-card "A simple counter" [:current]])
(r/render [app] (js/document.getElementById "app"))
#!bash
# using https://github.com/plexus/chestnut
lein new chestnut try-clojure -- --reagent
(ns try-clojure.db
(:require
[matchbox.core :as m]))
(defn connect
[url]
(let [root (m/connect url)]
(m/auth-anon root)
root))
(defn sync
[store root path]
(let [cursor (m/get-in root path)]
(m/listen-to
cursor :value
(fn [[event-type data]]
(if data
(reset! store data)))))
store)
(defn update!
[root path func]
(let [cursor (m/get-in root path)]
(m/swap! cursor func)))
[matchbox "0.0.9"]
[cljsjs/react-mdl "1.5.4-1"]
(ns try-clojure.ui
(:require
[cljsjs.react-mdl]))
(def Card js/ReactMDL.Card)
(def CardTitle js/ReactMDL.CardTitle)
(def CardText js/ReactMDL.CardText)
(def CardActions js/ReactMDL.CardActions)
(def CardMenu js/ReactMDL.CardMenu)
(def IconButton js/ReactMDL.IconButton)
(def Spinner js/ReactMDL.Spinner)
(defn counter-card
[title counter inc-fn dec-fn]
[:> Card {:shadow 0 :style {:width 512 :margin "auto"}}
[:> CardTitle title]
[:> CardText
[:h3
(if counter
counter
[:> Spinner])]]
[:> CardActions {:border true :style {:padding 20}}
"This is a simple counter backed by Firebase.
Click the plus and minus buttons to change the counter."]
[:> CardMenu
[:> IconButton {:onClick dec-fn :name "remove" :colored true}]
[:> IconButton {:onClick inc-fn :name "add" :colored true}]]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment