Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
318f4a1
commit 4ada2f4
Showing
3 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,3 +17,5 @@ dev-resources/public/js/*.js | |
*.*~ | ||
.DS_Store | ||
dev-resources/.DS_Store | ||
testing/resources/public | ||
project/.generated |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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))))) | ||
|
||
|
||
|
||
|
||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)))))))) |