Fork me on GitHub
#clojurescript
<
2019-02-07
>
cmal09:02:58

Hi, how to change the content of one element of HTMLCollection using ClojureScript? I just want to translate something like document.getElementsByTagName('style')[0].innerHTML="xxx"; to ClojureScript.

thheller09:02:49

(let [el (-> (js/document.getElementsByTagName "style")
             (aget 0))]
  (set! (.-innerHTML el) "xxx"))

👆 5
👍 5
joelsanchez10:02:13

also (aset (js/document.getElementsByTagName "style") 0 "innerHTML" "xxx")

thheller10:02:57

yeah but that is abusing aset for something it is not supposed to do since innerHTML is not an array

thheller10:02:30

aset really should only have numeric indexes as args with a final value

joelsanchez10:02:23

looking at the call to js* I don't think it would be a problem

joelsanchez10:02:29

(js* "(~{}[~{}][~{}] = ~{})" (js/document.getElementsByTagName "style") 0 "innerHTML" "xxx")

joelsanchez10:02:36

might not be the intended purpose but...

thheller10:02:06

that is not the point. yes it generates the correct code since it happens to be the same JS code

cmal10:02:17

I did this: (let [xx (some-> js/document (.getElementsByTagName "style") array-seq first .-innerHTML)] .. ) ... and when xx is not empty, do (set! xx "xxx")

thheller10:02:06

but that won't work? xx will be a string so using set! on it won't update the actual element

cmal13:02:05

Thanks, changed to (let [xx (some-> js/document (.getElementsByTagName "style"))] ..) and then (set! (-> xx array-seq first .-innerHTML) "xxx")

javi14:02:20

I am exploring extend-via-metadata and I am getting this behavior:

(defprotocol Component
  :extend-via-metadata true
  (start [component]))

(def component (with-meta {:name "db"} {`start (constantly "started")
                                        `clojure.core.protocols/datafy (fn [x] (:name x))}))

(satisfies? clojure.core.protocols.Datafiable component)
;; => true
(satisfies? Component component)
;; => false
So when extending with datafy protocol i get true when checking if the data satisfies? the protocol. However with a custom protocol with :extend-via-metadata true I am getting false in the satisfies? check. Is there something I am missing in the implementation? thanks

Alex Miller (Clojure team)15:02:37

In Clojure, satisfies? doesn’t check metadata extensions, not sure about cljs

Jimmy Miller16:02:33

Just to expand on the answer as to why satisfies seems to work for Datafiable. Datafiable is implemented on Object. https://github.com/clojure/clojure/blob/28b87d53909774af28f9f9ba6dfa2d4b94194a57/src/clj/clojure/core/protocols.clj#L191-L192 This even works.

(satisfies? clojure.core.protocols/Datafiable 2)
;; => true

javi17:02:39

thanks guys, makes sense.

Mario C.17:02:22

Is it okay to have multiple puts (`>!`) within a go block or should each put be within its own go-block? If the answer depends then what are some things I should consider before doing one over the other?

Mario C.18:02:05

@jkr.sw Thanks! I originally had each put within a go-block but I was thinking about it and reasoned that since, the way it was currently set up, everything was happening synchronously I might as well do them all within one go block. But wasn't sure if this was proper

henryw37419:02:04

I'm trying to write some code (to run at compilation time) that will only do a require if a library is present. I thought of trying something like this:

(defmacro include-if-present []
  (when (io/resource "foo/bar.cljc")
    '(require 'foo.bar)))
but it doesn't seem to work and the docs for cljs.core/require suggest it's 'for the repl only' .. not sure what restrictions that implies exactly. https://clojurescript.org/guides/ns-forms#_the_require_and_require_macros_macros Any ideas please let me know.

lilactown20:02:52

CLJS doesn't have dynamic require in general

Ramzi23:02:09

Has anyone gone through LearnReagent? I'm trying to build at the increments with no luck.