Skip to content

Commit

Permalink
initial setup for view bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
ckirkendall committed Dec 23, 2013
1 parent 318f4a1 commit 4ada2f4
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -17,3 +17,5 @@ dev-resources/public/js/*.js
*.*~
.DS_Store
dev-resources/.DS_Store
testing/resources/public
project/.generated
30 changes: 30 additions & 0 deletions src/cljs/enfocus/bind.cljs
@@ -0,0 +1,30 @@
(ns enfocus.bind
(:require
[enfocus.core :as ef :refer [from at set-attr get-attr]]))

(defn- build-key [id]
(str "__EVB:" id))

(defn bind-view-watch-fn [id render-func]
(fn [ctx ref oval nval]
(let [node (.getElementById js/document id)]
(if node
(render-func node nval)
(remove-watch ref (build-key id))))))



(defn bind-view [atm render-func]
(fn [node]
(let [id (from node (get-attr :id))
nid (if (empty? id) (gensym "_EVB_") id)]
(when-not (= id nid) (at node (set-attr :id nid)))
(render-func node @atm)
(add-watch atm
(build-key nid)
(bind-view-watch-fn nid render-func)))))





34 changes: 34 additions & 0 deletions test/cljs/enfocus/bind_test.cljs
@@ -0,0 +1,34 @@
(ns enfocus.bind-test
(:require
[enfocus.core :as ef :refer [at from content get-text]]
[enfocus.bind :as bind :refer [bind-view]]
[cemerick.cljs.test :as t])
(:require-macros
[enfocus.macros :as em]
[cemerick.cljs.test :refer (is are deftest testing use-fixtures)]))

(defn each-fixture [f]
;; initialize the environment
(let [div (.createElement js/document "div")
pc (.createElement js/document "p")]
(.setAttribute div "id" "test-id")
(.setAttribute div "class" "test-class")
(.setAttribute pc "class" "test-class")
(.appendChild div pc)
(.appendChild (.-body js/document) div)
;; execute the unit test
(f)
;; clear the environment
(.removeChild (.-body js/document) div)))

(use-fixtures :each each-fixture)

(deftest bind-view-test
(testing "binding a view to an atom"
(let [atm (atom "initial")]
(at "#test-id" (bind-view atm #(at %1 (content %2))))
(testing "initial value set"
(is (= "initial" (from "#test-id" (get-text)))))
(testing "updated value set"
(reset! atm "updated")
(is (= "updated" (from "#test-id" (get-text))))))))

0 comments on commit 4ada2f4

Please sign in to comment.