Last active
January 6, 2016 18:27
-
-
Save tylermorten/24ba8c404708c452b0de to your computer and use it in GitHub Desktop.
Routing in om.next with secretary?
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
(require '[secretary.core :refer-macros [defroute]] | |
'[om.next :as om :refer-macros [defui]] | |
'[om.dom :as dom]) | |
(def app-state {:routing {:routing/name :home :routing/id ""} | |
:persons [{:id 1 :person/name "Test 1"} | |
{:id 2 :person/name "Test 3"}]}) | |
(defn update-route [location id] | |
(om/transact! reconciler | |
`[(routing/navigate | |
{:routing/name :main | |
:routing/location ~location | |
:routing/id ~id})])) | |
(defui ^:once Routing | |
static om/IQuery | |
(query [_] | |
[:routing/name :routing/location :routing/id])) | |
(defui Person | |
static om/Ident | |
(ident [this {:keys [id]}] | |
{:person/by-id id}) | |
static om/IQuery | |
(query [_] | |
[:person/name :id]) | |
Object | |
(render [this] | |
(let [{:keys [person/name id]} (om/props this)] | |
(dom/h3 nil "Hello " name " with ID " id)))) | |
(def person-view (om/factory Person)) | |
(defui RootView | |
static om/IQuery | |
(query [_] | |
[{:routing (om/get-query Routing)} | |
{:persons (om/get-query Person)}]) | |
Object | |
(render [this] | |
(let [{:keys [routing persons] :as props} (om/props this) | |
person-id (get routing :routing/id) | |
norm-data (om/tree->db this props true) | |
person (get-in norm-data [:person/by-id person-id])] | |
(dom/div nil | |
(person-view person)))))) | |
;; Secretary route | |
(defroute person-view "/persons/:id" [id] | |
(om/add-root! reconciler | |
RootView | |
(js/document.getElementById "app")) | |
(update-route :person-view (js/parseInt id))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment