This commit is contained in:
Dan Holmsand 2015-09-08 00:12:41 +02:00
parent b520dfd6be
commit df464eda35
3 changed files with 34 additions and 24 deletions

View File

@ -20,18 +20,19 @@
:alt "Fork me on GitHub"
:src "https://s3.amazonaws.com/github/ribbons/forkme_left_orange_ff7600.png"}]])
(def index-page "index.html")
(def news-page "news/index.html")
;; (def index-page "index.html")
;; (def news-page "news/index.html")
(tools/register-page index-page [#'intro/main]
"Reagent: Minimalistic React for ClojureScript")
(tools/register-page news-page [#'news/main]
"Reagent news")
;; (tools/register-page index-page [#'intro/main]
;; "Reagent: Minimalistic React for ClojureScript")
;; (tools/register-page news-page [#'news/main]
;; "Reagent news")
(defroute "/" [] (dispatch [:content [#'intro/main]]))
(defroute main-page "/index.html" [] (dispatch [:content [#'intro/main]]))
(defroute news-p "/news/index.html" [] (dispatch [:content [#'news/main]]))
(defroute news-page "/news/index.html" [] (dispatch [:content [#'news/main]]))
(tools/reg-page (main-page))
(tools/reg-page (news-p))
(tools/reg-page (news-page))
(defn demo []
[:div
@ -39,7 +40,7 @@
[:ul.nav
[:li.brand [link {:href (main-page)} "Reagent:"]]
[:li [link {:href (main-page)} "Intro"]]
[:li [link {:href (news-p)} "News"]]
[:li [link {:href (news-page)} "News"]]
[:li [:a github "GitHub"]]]]
@test-results
[tools/page-content]

View File

@ -11,7 +11,7 @@
(def title "News in 0.5.0")
(declare main)
(defroute path "/news/news050.html" [] (dispatch [:content [#'main]]))
(defroute path "/news/news050.html" [] (dispatch [:content [#'main] title]))
(tools/reg-page (path))

View File

@ -16,19 +16,20 @@
(declare page-content)
(defn rswap! [a f & args]
;; Like swap!, except that recursive swaps are ok
;; Roughly like swap!, except that recursive swaps are ok
(let [fs (if-some [arr (.-rswapfs a)]
arr
(set! (.-rswapfs a) (array)))]
(.push fs #(apply f % args))
(if (< 1 (.-length fs))
(swap! a identity)
(loop []
(let [s (swap! a (aget fs 0))]
(.shift fs)
(if (-> fs .-length pos?)
(recur)
s))))))
nil
(let [f' (fn [state]
(let [s ((aget fs 0) state)]
(.shift fs)
(if (-> fs .-length pos?)
(recur s)
s)))]
(swap! a f')))))
;;; Configuration
@ -44,6 +45,7 @@
:js-file "js/main.js"
:js-dir "js/out"
:main-div "main-content"
:default-title "Reagent"
:allow-html5-history false}))
(defonce page (r/atom "index.html"))
@ -54,17 +56,24 @@
(defn demo-handler [state [id v1 v2 :as event]]
(case id
:content (do
(assoc state :main-content v1))
(let [title (or v2 (:default-title @config) "")]
(when r/is-client
(r/next-tick #(set! js/document.title title)))
(assoc state
:main-content v1
:title title)))
:set-page (do (secretary/dispatch! v1)
(assoc state :page v1))
:goto-page (do
(.setToken history v1)
(assoc state :page v1))
(when r/is-client
(.setToken history v1 false)
(r/next-tick #(set! js/document.body.scrollTop 0)))
(recur state [:set-page v1 v2]))
state))
(defn dispatch [event]
;; (r/next-tick #(rswap! config demo-handler event))
(dbg event)
(rswap! config demo-handler event)
(:main-content @config)
nil)
(defn reg-page [url]
@ -73,7 +82,7 @@
(defn init-history []
(when-not history
(doto (set! history (History.))
(evt/listen hevt/NAVIGATE #(secretary/dispatch! (.-token %)))
(evt/listen hevt/NAVIGATE #(dispatch [:set-page (.-token %)]))
(.setEnabled true))))