Skip to content

Instantly share code, notes, and snippets.

@tolitius
Last active August 29, 2015 13:56
Show Gist options
  • Save tolitius/9318463 to your computer and use it in GitHub Desktop.
Save tolitius/9318463 to your computer and use it in GitHub Desktop.
DOM selector observes all the changes made to the element, no matter the source
;; this is a ClojureScript REPL that is connected to a browser
;; (here is how it's connected: https://github.com/tolitius/wracer/blob/master/docs/README.md)
;; "sel1" here is a "dommy" library selector: e.g. similar to "$" in JQuery
wracer.repl=> (def $e-names (sel1 :.e-names))
#<[object HTMLDivElement]>
wracer.repl=> (inner-html $e-names)
<p class="e-name">Bing</p>
<p class="e-name">Google</p>
<p class="e-name">Yahoo</p>
;; adding an element from within the REPL
wracer.repl=> (dom/append! $e-names [:.repl-added "more from repl"])
#<[object HTMLDivElement]>
wracer.repl=> (inner-html $e-names)
<p class="e-name">Bing</p>
<p class="e-name">Google</p>
<p class="e-name">Yahoo</p>
<div class="repl-added">more from repl</div> ;; here is it. it is seen using the same "$e-names" reference
;; now we go out to the browser and add another element there
wracer.repl=> (inner-html $e-names)
<p class="e-name">Bing</p>
<p class="e-name">Google</p>
<p class="e-name">Yahoo</p>
<div class="repl-added">more from repl</div>
<div class="browser-added">more from browser</div> ;; which is again observed and accessed by the same "$e-names" reference
;; ------------------------------------------
;; quick & dirty, but useful in example above
;; (defn inner-html [el]
;; (print (clojure.string.replace
;; (.-innerHTML el) #"><" ">\n<")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment