Hiding a element in Clojurescript Dommy
-
23-12-2019 - |
Question
I'm trying to hide a element in the ClojureScript more specifically the Dommy library. This is what I got so far:
ClojureScript:
(ns fucking_about
(:require
[dommy.utils :as utils]
[dommy.core :as dommy])
(:use-macros
[dommy.macros :only [node sel sel1]]))
(map dommy/toggle! (sel :.hide))
HTML:
<html>
<head><title></title></head>
<body>
<ul>
<li class="show">Derp</li>
<li class="hide">Herp</li>
<li class="show">ies!</li>
</ul>
<script type="text/javascript" src="script.js"></script>
</body>
</html>
But I get no results when opening the site in a browser and I have no idea why :< and I cannot find any documentation on ClojureScript beyond the Github page.
Solution
The thing is that map
returns a lazy sequence of elements. Lazy sequence is some kind of unrealized promise of what you can get. You want your sequence to produce some side effects. To make it work you just need to realize it.
Problem is trivial. You have two choices:
1) Realize your lazy sequence using doall
function, like that:
(doall (map dommy/toggle! (sel :.hide)))
That’s exactly goal of doall
: walk through entire sequence and force any possible effects.
2) Use doseq
which is meant to produce side effect, like that:
(doseq [el (sel :.hide)]
(dommy/toggle! el))
And that’s all! Both ways you should get the same result.
Any more questions?