Fork me on GitHub
#clojurescript
<
2015-07-27
>
bbloom00:07:56

ok cljs people, i have some js questions regarding event handling - maybe some folks can help me w/ my design

bbloom00:07:07

preventDefault vs stopPropagation

bbloom00:07:22

i know the difference, but i’m having a hard time articulating when i’d want one without the other

bbloom00:07:33

i’ve had to use preventDefault on a-tags and such, as well as stopPropagation on click events for nested things

bbloom00:07:51

you can use stopPropagation w/o preventDefault in many cases b/c there is no interesting default to prevent

bbloom00:07:19

can somebody describe to me a case where they’ve needed to preventDefault BUT NOT stop propagation, or vice versa, in their om/react/whatever apps?

bbloom00:07:22

otherwise, can people confirm that they’ve never needed that?

wilkerlucio00:07:48

@bbloom: one case that you can use stopPropagation without preventDefault is on modal, let'

wilkerlucio00:07:28

@bbloom: I'm trying to understand why are you looking for a case like that

bbloom00:07:44

i’m hoping to prove that i don’t need that case simple_smile

wilkerlucio00:07:48

because preventDefault and stopPropagation have clear distinct functions I think

wilkerlucio00:07:05

I often use preventDefault without stopPropagation

bbloom00:07:32

is that b/c you WANT propagation, or because the propagation is harmless?

wilkerlucio00:07:37

and I find myself having to use stopPropagation when handling events inside of the modal (usually I stop the propagation at the modal root element)

bbloom00:07:37

in my experience, it’s the latter

wilkerlucio00:07:03

I think makes sense to always propagate, unless some special cases (like modals)

wilkerlucio00:07:36

because if you attach an event on a parent element (let's say a click), when I click on a child element, I expect the child and the parent events to be fired

wilkerlucio00:07:48

when I click on the child*

wilkerlucio00:07:06

if you stopPropagation on the child, the parent will never receive the event

lvh00:07:41

I’ve had that on grid games; where clicking a thing on the grid does a thing but the parent still wants to see the event simple_smile

bbloom00:07:49

but if a child handles it, like say a button on a selectable row, then you want propagation to stop

bbloom00:07:04

if you click the row, it toggles the selection, but if you click the button, it shouldn’t change the selection

wilkerlucio00:07:12

that depends on how you are structuring it

lvh00:07:19

I may have done it elsewhere as well; that one just comes to mind because that’s where I learned the difference simple_smile

wilkerlucio00:07:49

a more common example that I can think is nested elements that have mouseover/mouseout events

bbloom00:07:50

seems to me like it’s complecting capture and bubbling

bbloom00:07:01

if you want an event regardless of child handling, you should capture & get the event on the way down

bbloom00:07:03

not on the way up

wilkerlucio00:07:37

but the events are fired from bottom up, how would you get it before on the parent?

wilkerlucio00:07:02

sorry, I think I understand you now

wilkerlucio00:07:23

yeah, I agree with you that you can surely do it without ever propagating, I guess I'm just biased because I'm really used to think on events this way

bbloom00:07:10

so i’m implementing my own event routing that should be more functional. my current thought (for bubbling only at the moment) is to have (defn handler [event & _] event) would be the default handler…. the return value is nil for “stop propagation AND prevent default” but you can do something like (defn handler [event & _] :new-event) to prevent-default by rewriting the event in to your domain

bbloom00:07:27

so you’d rewrite a :click to a :submit or a :submit to a :signup

bbloom00:07:53

the _ represents the part you can’t rewrite. i’m not sure what is in there yet, but i think minimally it would include the “route”, including which component was clicked or handled the events, etc

wilkerlucio00:07:37

I just think some people went there before (like React, and jQuery) and they are currently deprecating the "return nil/false" for stop both, I don't know the real reason but I guess is to avoid confusion (I guess because by then the user of your lib will have to know your decision about what to do with the response)

bbloom00:07:23

well return false is weird for javascript b/c you have to explicitly return something & ‘false’ isn’t exactly self-explanitory

bbloom00:07:33

“returns the event to bubble” seems easy to explain

wilkerlucio00:07:58

sure, I actually like the convenience simple_smile

wilkerlucio00:07:17

I'm just poiting it out because React really annoys me even when I return false by accident there, hehe

bbloom00:07:32

ok, well, i’m going to explore this route (heh) a bit, but if anybody has any more thoughts on it, feel free to chime in

bbloom06:07:15

there’s still a fair amount of low hanging fruit, but that’s fast enough now that i can totally ignore the vdom impl perf-wise and work on the component layer

akiel10:07:35

is it possible to write a macro with generates different code for cljs than for clj? i can’t use reader conditionals because the macro is used by the clojure cljs compiler and so read as clj.

cfleming12:07:41

@bbloom: Glad to see you couldn’t resist cljs-vdom in the end simple_smile

curtosis15:07:40

luminus is giving me agita. cljsbuild produces js resources which the server doesn't serve; figwheel says it compiled resources/public/js/app.js but in fact has deleted the js directory. sigh.

curtosis17:07:44

... and now it's working. The only change was I rebooted (for other reasons). Fun! simple_smile

colin.yates18:07:07

@curtosis: Windows by any chance? (sorry, couldn’t resist)

curtosis18:07:32

@colin.yates: nah, Yosemite. simple_smile Not sure what I did to get my shell so wedged, but so it goes.

shaunlebron21:07:48

@dnolen: just wanted to raise the issue of cljs.js possibly being confused with cljsjs packages

shaunlebron21:07:19

cljs.bootstrap could be an alternative

dnolen21:07:40

@shaunlebron: already thought about that but it’s a bit too long

dnolen21:07:15

I’m also not concerned about confusion either, we’ve had decent errors about namespaces & definitions that don’t exist for a very long time.

dnolen21:07:11

cljs.js is also not really something anyone should be reaching for without a really, really good reason

dnolen21:07:21

i.e. not for apps the way cljsjs.foo is

shaunlebron21:07:53

that makes sense

dnolen21:07:22

right now cljs.js creates about 5MB of JavaScript

dnolen21:07:01

I’m far more concerned about people abusing eval-str than any confusion with CLJSJS

shaunlebron21:07:44

I’m not sure how I feel about discouraging the use of things through obscurity

shaunlebron21:07:06

you mentioned some time ago that some things shouldn’t be documented to discourage use of some things

shaunlebron21:07:16

just a bigger topic I have to think about for the website

dnolen21:07:24

cljs.js isn’t going to be obscure

dnolen21:07:39

it’s a thing, a footgun thing that can be used for awesome or for messes

shaunlebron21:07:32

right, it’ll be documented, but it’s namespace is non-descriptive I think

shaunlebron21:07:35

but you’ve addressed the confusion point already

dnolen21:07:51

@shaunlebron: yeah I’m not really concerned about the non-descriptive part

dnolen21:07:58

the idea was cljs.js and cljs.jvm

shaunlebron21:07:31

right, I’ve seen the “ClojureScript JS” and “ClojureScript JVM” terms to describe the compiler split

dnolen21:07:41

the namespace is pretty low-level, so far the only people using it are people who are building REPLs or tooling

dnolen21:07:50

it doesn’t need to be descriptive for any other kind of user

shaunlebron21:07:13

@dnolen: btw, nice txjs talk, really enjoyed it

dnolen22:07:05

@shaunlebron: thanks! hard to talk about CLJS in 25 minutes to a non-Clojure audience

shaunlebron22:07:56

@dnolen: yeah the talk about change in the beginning was really connecting. the flurry of tool demos was still good to show its current state I think

shaunlebron22:07:24

25 minutes, haha, too short

dnolen22:07:38

@shaunlebron: yeah the audience was definitely impressed by the demos - it’s really not that good for JavaScript at all

dnolen22:07:02

being able to demo 3 different kinds of environments that all the work the same is no small feat

dnolen22:07:12

thanks to the CLJS community we’re moving far fast

dnolen22:07:09

(and that’s ignoring this stuff also works in Rhino which is ancient as well as Nashorn)

pmooser22:07:57

Is it a known thing that cljs browser-connected REPLs generate stack traces that contain javascript line numbers as opposed to cljs line numbers? I'm using piggieback with weasel ...

shaunlebron22:07:59

👏 yeah that was really good to see all those environments working back to back

dnolen22:07:18

@pmooser: piggieback and weasel are 3rd party tooling

dnolen22:07:30

@pmooser: also you need to make sure the browser environment is configured correctly (usually you need devtools console to be open)

dnolen22:07:01

as well as the corresponding “Use JavaScript Source Maps" setting enabled

pmooser22:07:53

Thanks dnolen - I'll keep digging. I realize this is probably due to some configuration option. Maybe I'll try to move to the stock browser-connected REPL and go from there.

pmooser22:07:10

Ah, I remember now - I'm using piggieback because cider seems to need it for cljs. I'll investigate alternatives.

dnolen22:07:05

@pmooser: people have got cider working but it seems far finickier than the alternatives

dnolen22:07:22

cider + ClojureScript to be specific. cider + Clojure seems fine.

pmooser22:07:58

@dnolen: Yeah, I use it with clojure with no issues. Just trying to find a nice way of working with cljs with a repl - it might be best for me to ditch cider for cljs to reduce the number of moving parts.

dnolen22:07:51

@pmooser: inf-clojure works great, I suspect in the next 6 months cider will work quite well with ClojureScript w/o so many moving parts

dnolen22:07:56

but’s not going to be a short trip

dnolen22:07:46

also I keep saying if people will listen but I’m going on month 6 with Cursive w/ no nREPL at all

dnolen22:07:54

I now have zero problems

pmooser22:07:32

Cursive seems excellent but I'm not sure I can get away from emacs ... I mean it's possible obviously but I really like parts of it. I'm not enamored of the clojure-specific tooling due to its highly volatile and magical nature. Maybe I'll give cursive a try. I met colin at clojure/west and he was a hell of a nice guy.

dnolen22:07:49

I was an Emacs snob, IntelliJ appears to be designed with a pretty good understanding of what’s good about Emacs (sans endless configuration)

dnolen22:07:56

the only thing I miss is org-mode

dnolen22:07:40

my only real complaint with IntelliJ is that you have to be a bit careful w/ project configuration on a slower machine due to constant project indexing.

shofetim22:07:55

@dnolen: I don't want to start a flame war, but I am very curious, why move from emacs to cursive? Was there a particular feature that cursive had and emacs lacked?

dnolen22:07:27

@shofetim: I’ve been doing Clojure for 7 years

dnolen22:07:48

Cider went through a terrible unstable phase and was behind swank-clojure by 3 years in terms of features

dnolen22:07:58

never had a debugger that ever worked

dnolen22:07:45

IntelliJ has world class JavaScript support, Cursive has accurate Clojure debugging, eval in frame, YourKit integration, Node.js standard library indexing, trivial to index Google Closure

dnolen22:07:49

could go on forever really

dnolen22:07:57

it makes what I was doing in Emacs look primordial

shofetim22:07:58

shofetims scratches head and feels primordial. Ok, I will have to poke at this cursive thing and see if it drives better then emacs for me too.

shaunxcode23:07:02

the question is has cursive come out of the paredit uncanny valley?

shaunxcode23:07:11

That is my only hang up that keeps me in emacs

dnolen23:07:24

@shaunxcode: the only acceptable paredit I’ve used besides Emacs

dnolen23:07:40

the other import thing for a long time Emacser - sticky selection

dnolen23:07:02

this alone made me drop pretty much everything else until Cursive

dnolen23:07:05

can’t reprogram these hands

pmooser23:07:05

@dnolen I think intellij is a nice environment, but my experience with both eclipse and intellij is that they make emacs seem rather "lean and mean" which is quite an accomplishment. I'm sure I could adapt ...

dnolen23:07:33

@pmooser: from what I can tell most people never actually take the time to configure IntelliJ

dnolen23:07:40

my IntelliJ is just tabs, no chrome at all

pmooser23:07:03

Yeah, OK ok I will give cursive a try. I've been doing clojure forever but I shouldn't get stuck on old tooling.

dnolen23:07:36

and typing in a syntax highlighted 10,000 line ClojureScript standard library isn’t any faster in Emacs

nullptr23:07:05

While I’m a very happy Emacs user, I am extremely excited about the potential effect of the more IDE-like options for clj/cljs adoption in the broader community — it’s really good for all of us

bensu23:07:32

For those interested, I just released a snapshot version of https://github.com/bensu/doo that allows for :optimizations :none and autocompile while testing. Feedback welcome!