Skip to content

Instantly share code, notes, and snippets.

@haywoood
Last active January 2, 2016 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haywoood/8320322 to your computer and use it in GitHub Desktop.
Save haywoood/8320322 to your computer and use it in GitHub Desktop.
(ns test.core
(:require-macros [cljs.core.async.macros :refer [go]])
(:require [cljs.core.async :refer [<! chan put! sliding-buffer]]
[om.core :as om :include-macros true]
[om.dom :as dom :include-macros true]))
(def app-state (atom {:default-project-id 5
:projects
[{:id 1 :title "proj 1"}
{:id 2 :title "proj 2"}
{:id 3 :title "proj 3"}
{:id 4 :title "proj 4"}
{:id 5 :title "proj 5"}]}))
(enable-console-print!)
(defn set-active-project-id [id app]
(om/update! app assoc :active-project-id id))
(defn handle-click [e item owner chan]
(put! chan (:id item)))
(defn project-view [the-project owner]
(om/component
(dom/div nil (:title the-project))))
(defn project [the-project owner opts]
(reify
om/IRender
(render [_]
(dom/li
#js {:onClick (om/bind handle-click the-project owner (:chan opts))}
(str (:title the-project) " i'm active: " (:is-active? the-project))))))
(defn sidebar [projects owner opts]
(println "1 " (:active opts)) ; this will log correctly, but be one value behind
(om/component
(println "2 " (:active opts)) ; never logs after initial page load
(dom/div nil
(dom/p nil (str "active id: " (:active opts))) ; never updates after initial page load
(dom/ul nil
(om/build-all project
projects
{:opts opts :key :id
:fn (fn [proj]
(println "3 " (:active opts)) ; never logs after initial page load
(cond-> proj
(= (:active opts) (:id proj)) (assoc proj :is-active? true)))})))))
(om/root app-state
(fn [app owner]
(let [aid-chan (chan)]
(reify
om/IWillMount
(will-mount [_]
(go (while true
(let [aid (<! aid-chan)]
(set-active-project-id aid app)))))
om/IRender
(render [_]
(let [active-project-id (:active-project-id app)
projects (:projects app)
default (:default-project-id app)
current-project (if (nil? active-project-id)
(nth projects (dec default))
(first (filter #(= active-project-id (:id %)) projects)))]
(dom/div nil
(om/build sidebar projects {:opts {:chan aid-chan
:active (:active-project-id app)
:test-garbage "test garbage"
:root owner}})
(om/build project-view current-project)))))))
(.getElementById js/document "app"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment