2014-01-17 11:35:40 +00:00
|
|
|
|
(ns reagentdemo.news
|
2014-01-19 09:25:16 +00:00
|
|
|
|
(:require [reagent.core :as reagent :refer [atom]]
|
|
|
|
|
[reagent.debug :refer-macros [dbg println]]
|
|
|
|
|
[reagentdemo.syntax :refer-macros [get-source]]
|
2014-01-21 10:50:08 +00:00
|
|
|
|
[reagentdemo.page :refer [title link page-map]]
|
|
|
|
|
[reagentdemo.common :as common :refer [demo-component]]
|
2014-01-30 21:29:42 +00:00
|
|
|
|
[reagentdemo.news.async :as async]
|
2014-01-19 09:25:16 +00:00
|
|
|
|
[todomvc :as todomvc]))
|
2014-01-17 11:35:40 +00:00
|
|
|
|
|
2014-01-19 09:25:16 +00:00
|
|
|
|
(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)))
|
2014-01-17 11:35:40 +00:00
|
|
|
|
[:div
|
2014-01-19 09:25:16 +00:00
|
|
|
|
[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))}))
|
|
|
|
|
|
2014-01-20 10:37:57 +00:00
|
|
|
|
(defn undo-example []
|
2014-01-22 07:40:04 +00:00
|
|
|
|
(let [head "Cloact becomes Reagent: Undo is trivial"]
|
2014-01-20 08:42:16 +00:00
|
|
|
|
[:div.reagent-demo
|
2014-01-20 10:37:57 +00:00
|
|
|
|
[:h1 [link {:href undo-example} head]]
|
2014-01-20 08:42:16 +00:00
|
|
|
|
[title head]
|
2014-01-22 07:40:04 +00:00
|
|
|
|
[:div.demo-text
|
|
|
|
|
[:h2 "(reset! cloact-name \"Reagent\")"]
|
|
|
|
|
|
|
|
|
|
[:p "It turns out that ”Cloact” was a really, really bad
|
|
|
|
|
name. It made some people think about birds’ behinds, in
|
|
|
|
|
possibly unhealthy ways, which even Google suggested they
|
|
|
|
|
should."]
|
|
|
|
|
|
|
|
|
|
[:p "The new name is " [:strong "Reagent"] ", which hopefully
|
|
|
|
|
doesn’t bring with it the same disgusting connotations."]
|
|
|
|
|
|
|
|
|
|
[:p "The API is otherwise unchanged, so a simple
|
|
|
|
|
search-and-replace should suffice."]
|
|
|
|
|
|
|
|
|
|
[:h2 "Undo the easy way"]
|
|
|
|
|
|
|
|
|
|
[:p "To celebrate the undoing of the apparently disgusting name,
|
|
|
|
|
here is an example of how easy it is to add undo functionality
|
|
|
|
|
to Reagent components."]
|
|
|
|
|
|
|
|
|
|
[:p "It simply saves the old state whenever it changes, and
|
|
|
|
|
restores it when the button is clicked."]
|
|
|
|
|
|
|
|
|
|
[:p "The really nice thing about ClojureScript is that not only
|
|
|
|
|
is this easy and safe to do, courtesy of immutable data
|
|
|
|
|
structures, it is also efficient. ClojureScript figures out how
|
|
|
|
|
to represent ”changes” to maps and vectors efficiently, so that
|
|
|
|
|
you won’t have to."]
|
|
|
|
|
|
|
|
|
|
[undo-demo-cleanup]]]))
|
2014-01-20 10:37:57 +00:00
|
|
|
|
|
|
|
|
|
(defn main []
|
2014-01-30 21:29:42 +00:00
|
|
|
|
[:div
|
|
|
|
|
[async/main]
|
|
|
|
|
[undo-example]])
|
2014-01-20 10:37:57 +00:00
|
|
|
|
|
2014-01-21 10:50:08 +00:00
|
|
|
|
(swap! page-map assoc
|
2014-01-20 10:37:57 +00:00
|
|
|
|
"news/cloact-reagent-undo-demo.html" undo-example)
|