Permalink
Browse files

Add simple data storage to nodes

  • Loading branch information...
1 parent 9a56e1f commit 0f127a1fe8c1b26bf332cf8eae22cd20ec9082db @levand levand committed Mar 29, 2012
Showing with 49 additions and 1 deletion.
  1. +22 −0 src/cljs/domina.cljs
  2. +27 −1 test/cljs/domina/test.cljs
View
@@ -345,6 +345,28 @@
(fallback html-string)))
content)
+(defn get-data
+ "Returns data associated with a node for a given key. Assumes
+ content is a single node. If the bubble parameter is set to true,
+ will search parent nodes if the key is not found."
+ ([node key] (get-data node key false))
+ ([node key bubble] (let [m (.-__domina_data (single-node node))
+ value (when m (get m key))]
+ (if (and bubble (nil? value))
+ (when-let [parent (.-parentNode (single-node node))]
+ (get-data parent key true))
+ value))))
+
+(defn set-data!
+ "Sets a data on the node for a given key. Assumes content is a
+ single node. Data should be ClojureScript values and data structures
+ only; using other objects as data may result in memory leaks on some
+ browsers."
+ [node key value]
+ (let [m (or (.-__domina_data (single-node node)) {})]
+ (set! (.-__domina_data (single-node node))
+ (assoc m key value))))
+
;;;;;;;;;;;;;;;;;;; private helper functions ;;;;;;;;;;;;;;;;;
(defn- apply-with-cloning
@@ -3,7 +3,7 @@
prepend! detach! destroy! destroy-children! insert! insert-before!
insert-after! swap-content! style attr set-style! set-attr! styles attrs
set-styles! set-attrs! has-class? add-class! remove-class! classes
- text set-text! value set-value! html set-html! log-debug]]
+ text set-text! value set-value! html set-html! set-data! get-data log-debug]]
[domina.xpath :only [xpath]]
[domina.css :only [sel]]
[domina.events :only [listen! unlisten! remove-listeners! fire-listeners!]])
@@ -131,6 +131,15 @@
(standard-fixture)
(assert (= 1 (count (nodes (by-id "id1")))))))
+(add-test "look up node by id with context"
+ #(do (reset)
+ (append! (sel "body")
+ "<div><p><span>some text</span></p><p><span>more text</span></p></div>")
+ (assert (= 2 (count (nodes (-> (sel "body")
+ (sel "div")
+ (sel "p")
+ (sel "span"))))))))
+
(add-test "look up nodes by class"
#(do (reset)
(standard-fixture)
@@ -666,6 +675,23 @@
(append! (xpath "//body") child)
(assert (= child (single-node (xpath "//body/div")))))))
+
+(add-test "test that data works"
+ #(do
+ (reset)
+ (append! (xpath "//body") "<div id='testingdata'>hello</div>")
+ (let [data {:some "complex data"}]
+ (set-data! (by-id "testingdata") :my-impeccable-data data)
+ (assert (= data (get-data (by-id "testingdata") :my-impeccable-data))))))
+
+(add-test "test that data works with bubbling"
+ #(do
+ (reset)
+ (append! (xpath "//body") "<div id='outerdata'><div id='innerdata'>hello</div></div>")
+ (let [data {:some "complex data"}]
+ (set-data! (by-id "outerdata") :my-impeccable-data data)
+ (assert (= data (get-data (by-id "innerdata") :my-impeccable-data true))))))
+
(defn report
[test-results]
(reset)

0 comments on commit 0f127a1

Please sign in to comment.