Last active
March 3, 2016 16:40
-
-
Save mhuebert/0660003c82a39838c355 to your computer and use it in GitHub Desktop.
defroutes macro for ClojureScript/Secretary - quickly define many routes at once
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
(defmacro defroutes [& routes] | |
`(do | |
~@(map (fn [[[path] body]] | |
(let [has-args? (or (vector? (first body)) (map? (first body))) | |
body (if has-args? body (cons [] body))] | |
`(~'secretary.core/defroute ~path ~@body))) | |
(partition 2 (partition-by string? routes))))) |
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
(defroutes | |
"/" (if (signed-in?) | |
(set-view! :user/home) | |
(set-view! :app/landing)) | |
"/sign-in" (set-view! :auth/sign-in) | |
"/sign-out" (do (auth/sign-out) | |
(h/nav! "/")) | |
"/horse/:name" [name] (prn "This horse has a name: " name) ;; parameter destructuring works normally | |
"*" (set-view! :app/not-found)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment