 
 (defonce app-state (atom {:grids {}}))
 
-(defn cell-view [cursor _]
+(defn grids []
+  (om/ref-cursor (:grids (om/root-cursor app-state))))
+
+(defn cell-view [{:keys [cursor id]} _]
   (reify
     om/IRenderState
     (render-state [_ state]
       (dom/td #js {:onClick #(do
                                (om/update! cursor [(mod (inc (first cursor)) 2)])
-                               (go (>! (:set-state-ch state) (:ns state))))
+                               (go (>! (:set-state-ch state) id)))
                    :style #js {:width "20px"
                                :fontSize "20px"}}
              (str " " (first cursor))))))
 
 (declare grid-view)
 
-(defn track-view [cursor owner]
+(defn track-view [{:keys [cursor id]} owner]
   (reify
     om/IInitState
     (init-state [_]

         :get-state-ch get-state-ch}))
    om/IRenderState
    (render-state [_ state]
      (let [root (get (:grids cursor) "root")]
        (if root
          (dom/div #js {:style #js {:fontFamily "monospace"}}
                   (om/build grid-view root {:state (assoc state :ns "root")}))
          (dom/div nil "loading..."))))))
        
(om/root app-view app-state
  {:target (. js/document (getElementById "app"))})

                (dom/td nil
                 (dom/table nil
                  (apply dom/tr nil
                         (let [cursors (map #(hash-map :cursor %1 :id %2)
                                            (get cursor "beats")
                                            (repeat id))]
                           (om/build-all cell-view cursors {:state state})))))
                (dom/td nil
                        (when (= "grid" (get cursor "type"))
                          (let [id (get cursor "id")
                                sub-state {:set-state-ch (:set-state-ch state)
                                           :get-state-ch (:get-state-ch state)}]
                            (om/build grid-view id {:state sub-state})))))))))

(defn grid-view [id owner]
  (reify
    om/IInitState
    (init-state [_]
      {:grid-expanded false})
    om/IRenderState
    (render-state [_ state]
      (let [cursor (get (om/observe owner (grids)) id)]
        (dom/div #js {:style #js {:borderLeft "solid 3px #ccc"
                                  :paddingLeft "10px"}}
                 (if-not (:grid-expanded state)
                   (dom/div nil (dom/p #js {:onClick #(om/set-state! owner :grid-expanded true)
                                            :style #js {:color "#999"}}
                                       (str "[+] " id)))
                   (dom/div nil (dom/p #js {:onClick #(om/set-state! owner :grid-expanded false)
                                            :style #js {:color "#999"}}
                                       (str "[-] " id))
                            (apply dom/table nil
                                   (let [cursors (map #(hash-map :cursor %1 :id %2)
                                                      (get cursor "tracks")
                                                      (repeat id))]
                                     (om/build-all track-view cursors {:state state}))))))))))

(defn app-view [cursor _]
  (reify
