-
-
Save ibdknox/7a5b6722ccbaf7645525 to your computer and use it in GitHub Desktop.
todo cljs
This file contains 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 todo.core | |
(:require [pinot.html :as ph] | |
[pinot.html.tags :as tags] | |
[pinot.dom :as dom] | |
[pinot.events :as pe]) | |
(:require-macros [pinot.macros :as pm])) | |
(def todos (atom {1 "Get milk" | |
2 "Pay bills" | |
3 "Do stuff"})) | |
(defn add-todo [text] | |
(let [neue-key (inc (apply max (keys @todos)))] | |
(swap! todos assoc neue-key text) | |
[neue-key text])) | |
(defn remove-todo [id] | |
(swap! todos dissoc id)) | |
(pm/defpartial todo-item [[id item]] | |
[:li (tags/link-to {:class "remove" :id id} "#" item)]) | |
(pm/defpartial todo-form [] | |
(tags/form-to {:id "todoForm"} [:post "/todos"] | |
(tags/label "todoText" "Todo: ") | |
(tags/text-field "todoText") | |
(tags/submit-button {:class "submit"} "add todo"))) | |
(dom/append (dom/query "body") | |
(ph/html | |
[:div | |
(todo-form) | |
[:ul | |
(map todo-item @todos)]])) | |
(pe/on todo-form :submit | |
(fn [me e] | |
(let [text (dom/val (dom/query "#todoText")) | |
neue (add-todo text)] | |
(dom/append (dom/query "ul") (todo-item neue)) | |
(pe/prevent e)))) | |
(pe/on (dom/query "li") :click | |
(fn [me e] | |
(let [id (dom/attr me :id)] | |
(remove-todo id) | |
(dom/unappend me) | |
(pe/prevent e)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment