Last active
August 29, 2015 13:58
-
-
Save pleasetrythisathome/10420721 to your computer and use it in GitHub Desktop.
stab at multimethods for app view
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
(def app-state (atom {:section :main | |
:sections {:admin {:id :admin | |
:title "Admin"} | |
:main {:id :main | |
:title "Main"} | |
:other {:id :other | |
:title "Other"}}})) | |
(defn button | |
[{:keys [id title] :as section} owner] | |
(reify | |
om/IRender | |
(render [_] | |
(html | |
[:div {:class "button" | |
:on-click #(put! (om/get-shared owner :transact) [[:section] id])} | |
title])))) | |
(defmulti section-view :section) | |
(defmethod section-view :main | |
[app owner] | |
(reify | |
om/IRender | |
(render [_] | |
(html | |
[:div {:class "main"} | |
"This is the main section!"])))) | |
(defmethod section-view :admin | |
[app owner] | |
(reify | |
om/IRender | |
(render [_] | |
(html | |
[:div {:class "admin"} | |
"This is where you make shit up for your users!"])))) | |
(defmethod section-view :other | |
[app owner] | |
(reify | |
om/IRender | |
(render [_] | |
(html | |
[:div {:class "other"} | |
"the big mystery of life"])))) | |
(defn app-view | |
[{:keys [section sections] :as app} owner] | |
(reify | |
om/IWillMount | |
(will-mount [_] | |
(go | |
(while true | |
(let [[path val] (<! (om/get-shared owner :transact))] | |
(om/update! app path val))))) | |
om/IRender | |
(render [_] | |
(html | |
[:div {:class "app"} | |
[:div | |
(om/build-all button (vals sections) {:key :id})] | |
[:div | |
(om/build #(section-view %) app)]])))) | |
(om/root app-view app-state {:target (.getElementById js/document "ccs") | |
:shared {:transact (chan)}}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment