Skip to content

Instantly share code, notes, and snippets.

@haywoood
Created January 7, 2014 03:53
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/8294408 to your computer and use it in GitHub Desktop.
Save haywoood/8294408 to your computer and use it in GitHub Desktop.
(ns test.core
(:require [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"}]}))
(defn set-active-project-id [id owner]
(om/set-state! owner :active-project-id id))
(defn project-view [the-project owner]
(om/component
(dom/div nil (:title the-project))))
(defn project [the-project owner opts]
(let [is-active? (== (:id the-project) (:active-project-id opts))]
(om/component
(dom/li
#js {:onClick #(set-active-project-id (om/read the-project :id) (:root opts))}
(str (:title the-project) " i'm active: " is-active?)))))
(defn sidebar [projects owner opts]
(let [active-project-id (om/get-state (:root opts) :active-project-id)]
(om/component
(dom/div nil
(dom/ul nil
(om/build-all project projects {:key :id :opts {:root (:root opts)
:active-project-id active-project-id}}))))))
(om/root app-state
(fn [app owner]
(om/component
(let [active-project-id (om/get-state owner :active-project-id)
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 {: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