Fork me on GitHub
#re-frame
<
2015-11-18
>
kauko08:11:03

I have a couple of questions about structuring large re-frame applications. First, what’s a good way to name events? Second, what is a good folder structure? The wiki has a page on large applications, but the answer is basically to split your app into multiple “panels” = app-dbs, which is something I’d like to avoid.

kauko08:11:49

Let’s say I have a bunch of views and each of them have a delete-button. Should they all just dispatch events called :<view-name>-delete-pressed or something like that?

escherize08:11:04

@kauko: One thing that's more idiomatic than :namespace-delete-pressed is to use :namespace/delete-pressed. / is just a regular old character which is permitted in a keyword, but it signifies a namespace.

kauko08:11:39

Ah, obvious simple_smile

escherize08:11:39

I have a medium-large re-frame app... what we do is have handlers.cljs hold all the handlers, subs.cljs hold all the subscription, and the views are in a views directory ala views/this-component.cljs

escherize08:11:55

and with event naming, I think just unique names is best, and make it obvious what args a handler expects.

kauko08:11:17

I feel like subs and handlers.cljs grow really fast if you just have one of each

kauko08:11:19

in large apps

escherize08:11:12

Yeah, is there a downside to that?

escherize08:11:27

The upside is that all the handlers or subs are in 1 place each, and this is tooling dependant, but helm multi-occur lets me search through and find them very easily. Also if a sub or handler is used in multiple places I never have to decide where to put it.

escherize08:11:23

But I might be missing something simple_smile

kauko09:11:14

Sorry, was afk

kauko09:11:47

Wait, what do you mean by “sub or handler is used in multiple places”?

kauko09:11:07

I thought you can only have one handler per one event

kauko09:11:24

Also, I just think that having 1000+ loc files is something that should be avoided, and in apps the size I’m working on we would get there very quickly

kauko09:11:50

I don’t see why :some-view/typed-in-date-field should be in the same namespace as :some-totally-different-view/location-data-received

josesanch09:11:36

Hello, I'm trying to do an ajax search as you type component. What would be the best approach to this. Wrapping the dispatch event in a setTimeout / clearTimeout?

escherize10:11:28

I can see how it can be a good idea to register handlers in different namespaces (like ones that are parallel to the view in question). This is pedantic, but all the handlers are literally in the same "namespace" meaning (register-handler :continue-clicked) from different places will over-write one-another, which is why rightly opted for namespaced keywords

kauko11:11:23

Thanks for your comments, I really appreciate it simple_smile

escherize14:11:18

weird thought: you can actually use different clojure namespaces, with the ::a type of keyword.

escherize14:11:48

since for example

::a
;;=> :homing-pigeon.re-frame.handlers/a

escherize14:11:41

then naming them something like ::request, ::submit, ::input-validated might work nicely

kauko15:11:54

But I guess it would only help for one namespace. For example, I could have (register-handler ::foobar)in namespace handler.foobar, but in view.foobarI would have to do (dispatch :handler.foobar/foobar)

roberto15:11:36

I personally don’t have an issue with that. I guess it comes down to taste.

gadfly36116:11:38

@kauko you wouldn't need to do (dispatch :handler.foobar/foobar) if you require and alias the handler.foobar namespace.

(ns view.foobar
  (:require [handler.foobar :as f]))

(dispatch ::f/foobar)

danielcompton22:11:39

@kauko: you can pass parameters to handlers, so if you have a lot of similar handlers, you could combine them

danielcompton22:11:55

@escherize: keywords can have a namespace to them

danielcompton22:11:09

they are special, / isn’t just another character in a keyword

danielcompton22:11:48

cljs.user=> (namespace :myns/mykw)
"myns"
cljs.user=> (name :myns/mykw)
"mykw"

escherize23:11:23

you are correct @danielcompton. I always assumed clojure.core/namespace just did a sketchy string -> kw conversion. but TIL it's actually a piece of java. http://grepcode.com/file/repo1.maven.org/maven2/org.clojure/clojure/1.7.0/clojure/lang/Symbol.java#21

escherize23:11:11

;;Not how it works
(defn fake-namespace-pseudocode [kw] (->> kw name (re-seq #"/") first))