Fork me on GitHub
#hoplon
<
2017-07-18
>
mudphone04:07:43

I’m confused about when / where to write code to trigger js when an element is created. For example, attaching Bootstrap tooltips to elements created by a Hoplon defelem. Is there a pattern for this? It’s probably really simple and I’m just not seeing it.

mudphone04:07:33

I need the element to exist before I run the Bootstrarp .tooltip() fn, but I’m not sure where to run it. It definitely doesn’t work inside the defelem?

mudphone04:07:22

I’m wondering if there’s a way to trigger js to run after an element is loaded.

mudphone05:07:58

Maybe the right question to ask is, should I use when-dom for this?

mudphone05:07:58

Oh, I didn’t realize I could call jQuery methods on elements before they are added to the dom. That makes sense. 🙂

thedavidmeister08:07:58

@mudphone seems like you worked it out

thedavidmeister09:07:26

it's pretty common to be able to work with elements while they are detached

thedavidmeister09:07:37

but if not, when-dom is totally fine too

mudphone09:07:53

@thedavidmeister thank you, that’s really cool. I like it. Manipulating the elements before attaching to the DOM makes so much sense.

mudphone09:07:55

I don’t think I really understand how to use when-dom… (and on!, and do!) yet… is there a good way to learn more about that? I’ve read the guides.

thedavidmeister10:07:45

with-let is really handy for this

thedavidmeister10:07:07

(j/with-let [el (div "foo")]
 (.doSomething el))

thedavidmeister10:07:27

(let [el (div "foo")]
  (.doSomething el)
  el)

thedavidmeister10:07:10

so hoplon functions return dom elements detached from the document

thedavidmeister10:07:43

but i think that the html and body functions are special

thedavidmeister10:07:47

so when regular elements go into the html hoplon puts it in the document

dm310:07:55

also (doto ..) works if you’re only mutating the same element and it comes as the first argument

dm310:07:03

(doto (div "foo") (.doSomething))

thedavidmeister10:07:04

or maybe its in the page generation, actually i don't know exactly where it happens 😛

thedavidmeister11:07:06

ok yes, my example could just be a doto

thedavidmeister11:07:21

but with-let is still handy because you can do lots of stuff in there

dm311:07:25

it makes code more rhombusy

mudphone05:07:42

Is that a word? What does it mean? 😀

thedavidmeister11:07:36

@mudphone so when-dom just executes when the detached element gets attached

mudphone11:07:45

I think I used it (`when-dom`) by mistake, instead of with-let, and it retuned a 2

thedavidmeister00:07:21

@mudphone that's what a timeout returns

mudphone08:07:09

Hmm, interesting. So I was blocking somehow?

thedavidmeister11:07:00

@dm3 yeah you don't get dangling return values at the bottom of your big function

thedavidmeister11:07:54

on! and do! just bind event handlers and update html attributes or trigger events respectively

thedavidmeister11:07:05

hoplon automatically figures out which one you mean based on what you pass it

thedavidmeister11:07:59

(h/div :focus true) will trigger focus on that div via do!

thedavidmeister11:07:27

(h/div :focus #(prn "hi")) will respond to a focus event by prning "hi" via on!

thedavidmeister11:07:48

(h/div :data-focus true) will set the data-focus html attribute via do!