Add simple undo demo

This commit is contained in:
Dan Holmsand 2014-01-19 10:25:16 +01:00
parent ce9afb470c
commit 0a0dbdee7d
3 changed files with 49 additions and 5 deletions

View File

@ -100,7 +100,7 @@
[slider {:value bmi :min 10 :max 50 :param :bmi}]]])) [slider {:value bmi :min 10 :max 50 :param :bmi}]]]))
(def funmap (-> "reagentdemo/intro.cljs" get-source common/fun-map)) (def funmap (-> "reagentdemo/intro.cljs" get-source common/fun-map))
(defn src-for [defs] (common/src-for funmap defs)) (def src-for (partial common/src-for funmap))
(defn intro [] (defn intro []
(let [github {:href "https://github.com/holmsand/reagent"} (let [github {:href "https://github.com/holmsand/reagent"}

View File

@ -1,6 +1,49 @@
(ns reagentdemo.news (ns reagentdemo.news
(:require [reagent.core :as reagent :refer [atom]])) (:require [reagent.core :as reagent :refer [atom]]
[reagent.debug :refer-macros [dbg println]]
[reagentdemo.syntax :refer-macros [get-source]]
[reagentdemo.common :as common :refer [demo-component]]
[todomvc :as todomvc]))
(def funmap (-> "reagentdemo/news.cljs" get-source common/fun-map))
(def src-for (partial common/src-for funmap))
(def state todomvc/todos)
(def undo-list (atom nil))
(defn undo []
(let [undos @undo-list]
(when-let [old (first undos)]
(reset! state old)
(reset! undo-list (rest undos)))))
(defn undo-button []
(let [n (count @undo-list)]
[:input {:type "button" :on-click undo
:disabled (zero? n)
:value (str "Undo (" n ")")}]))
(defn todomvc-with-undo []
(add-watch state ::undo-watcher
(fn [_ _ old-state _]
(swap! undo-list conj old-state)))
[:div
[undo-button]
[todomvc/todo-app]])
(defn undo-demo []
[demo-component {:comp todomvc-with-undo
:src (src-for [:state :undo-list :undo :save-state
:undo-button :todomvc-with-undo])}])
(def undo-demo-cleanup
(with-meta undo-demo {:component-will-unmount
(fn []
(reset! undo-list nil)
(remove-watch state ::undo-watcher))}))
(defn main [] (defn main []
[:div [:div.reagent-demo
[:h2 "This should become news"]]) [:h1 "This should become news"]
[undo-demo-cleanup]])

View File

@ -8,7 +8,8 @@
"empty" "into" "assoc-in" "dissoc" "get-in" "when-not" "empty" "into" "assoc-in" "dissoc" "get-in" "when-not"
"filter" "vals" "count" "complement" "identity" "dotimes" "filter" "vals" "count" "complement" "identity" "dotimes"
"update-in" "sorted-map" "inc" "dec" "false" "true" "not" "update-in" "sorted-map" "inc" "dec" "false" "true" "not"
"="}) "=" "partial" "first" "second" "rest" "list" "conj"
"drop" "when-let" "if-let" "add-watch"})
(def styles {:comment {:style {:color "gray" (def styles {:comment {:style {:color "gray"
:font-style "italic"}} :font-style "italic"}}