Last active
December 25, 2015 21:22
-
-
Save lilrooness/5f5bc7ea95c3eb9d8713 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (ns counter.core | |
| (:require [clojure.browser.repl :as repl])) | |
| ;; (defonce conn | |
| ;; (repl/connect "http://localhost:9000/repl")) | |
| (enable-console-print!) | |
| (def ctr (atom {:count 0})) | |
| (defn increment [state] (let [count (:count state)] | |
| (assoc state :count (inc count)))) | |
| (defn decrement [state] (let [count (:count state)] | |
| (assoc state :count (dec count)))) | |
| (defn mkbutton [text, callback] | |
| (let [button (.createElement js/document "button") | |
| body (.-body js/document)] | |
| (set! (.-id button) text) | |
| (set! (.-innerText button) text) | |
| (set! (.-onclick button) callback) | |
| (.appendChild body button) | |
| )) | |
| (defn render [] | |
| (let [d (.getElementById js/document "display")] | |
| (set! (.-innerText d) (:count @ctr)))) | |
| (let [body (.-body js/document) | |
| display (.createElement js/document "h1")] | |
| (set! (.-id display) "display") | |
| (set! (.-innerText display) (:count @ctr)) | |
| (.appendChild body display)) | |
| (mkbutton "dec" (fn [e] ((swap! ctr decrement) (render)))) | |
| (mkbutton "inc" (fn [e] ((swap! ctr increment) (render)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment