Skip to content

Instantly share code, notes, and snippets.

@cgrand
Last active July 4, 2019 10:00
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 cgrand/bbb00513213d994b56fd6285b5fa5934 to your computer and use it in GitHub Desktop.
Save cgrand/bbb00513213d994b56fd6285b5fa5934 to your computer and use it in GitHub Desktop.
EZ todo
(ns enlivez.demo
(:require [enlivez.core :as ez]
[datascript.core :as d]))
(ez/deftemplate new-item []
:state {:db/id self
new-todo ""}
[:li
[:input {:value new-todo
:on-change (doto [[:db/add self ::new-todo (-> % .-target .-value)]] prn)}]
[:button {:disabled (= "" new-todo)
:on-click [{:item/title new-todo :item/done false}
[:db/add self ::new-todo ""]]} "Add!"]])
(ez/deftemplate todo []
[:ul
(new-item)
(ez/for {:db/id item [title done] :item/attrs}
:state {:db/id self
editing false
working-title ""}
:sort [done title]
[:li
[:input {:type :checkbox :checked done
:on-change [[:db/add item :item/done (not done)]]}]
[:span {:on-click (when (not editing) [{:db/id self ::editing true ::working-title title}])}
(ez/for [(= editing false)] title)
(ez/for [(= editing true)]
[:input {:value working-title
:on-change [[:db/add self ::working-title (-> % .-target .-value)]]
:on-blur [[:db/add self ::editing false]
[:db/add item :item/title working-title]]}])]])])
(defn show []
(let [conn @#'ez/conn]
(d/reset-conn! conn (d/empty-db (:schema @conn)))
(ez/mount todo (.-body js/document))
(:tx-data (d/transact! conn [{:item/title "something" :item/done false}
{:item/title "something else" :item/done false}]))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment