Last active
August 29, 2015 13:56
-
-
Save qbg/9254530 to your computer and use it in GitHub Desktop.
Editing weirdness
This file contains hidden or 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
(ns om-tut.core | |
(:require [om.core :as om :include-macros true] | |
[om.dom :as dom :include-macros true])) | |
(enable-console-print!) | |
(def app-state (atom {:text "Works", :data [{:text "Doesn't"}]})) | |
(defn handle-change | |
[e data edit-key owner] | |
(om/update! data edit-key (.. e -target -value))) | |
(defn commit-change | |
[owner] | |
(om/set-state! owner :editing false)) | |
(defn editable | |
[data owner {:keys [edit-key]}] | |
(reify | |
om/IInitState | |
(init-state [_] {:editing false}) | |
om/IRenderState | |
(render-state [_ {:keys [editing]}] | |
(let [text (get data edit-key)] | |
(dom/p nil | |
(if editing | |
(dom/input | |
#js {:value (om/value text) | |
:onChange #(handle-change % data edit-key owner) | |
:onKeyPress #(when (== (.-keyCode %) 13) | |
(commit-change owner)) | |
:onBlur (fn [e] (commit-change owner))}) | |
(dom/span nil | |
(om/value text) | |
(dom/button | |
#js {:onClick #(om/set-state! owner :editing true)} | |
"Edit")))))))) | |
(defn lister | |
[root owner] | |
(apply dom/div nil | |
(om/build-all editable (:data root) {:opts {:edit-key :text}}))) | |
;;; Works | |
(om/root | |
editable | |
app-state | |
{:target (. js/document (getElementById "app0")) | |
:opts {:edit-key :text}}) | |
;;; Doesn't | |
(om/root | |
lister | |
app-state | |
{:target (. js/document (getElementById "app1"))}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment