Fork me on GitHub
#clojurescript
<
2021-01-16
>
bibiki15:01:48

hi, I am going through chapter four of Reactive with ClojureScript Recipes and am having a little trouble with the following code:

(defn selectors []
  [:div
   [bind-fields
    [:select {:id :color}
     (for [opt ["black" "red" "blue"]] [:option {:key opt} opt])]
    state]
   [bind-fields
    [:select {:id :step}
     (for [opt [1 3 5]] [:option {:key opt} opt])]
    state]])

bibiki15:01:17

according to my understanding, the bind-fields function will make sure that the select widgets will reflect the value of :color and :step values in my state, but when I try it in the browser those two values do not get updated

bibiki15:01:43

would anyone know why that is? is that perhaps due to some version of the libraries or something?

bibiki17:01:58

apparently, this won't work for two reasons: 1. bind-fields needs {:field :list :id iddd} (note field->list specification. 2. selectors function should return another function that returns the hiccup-specified object, like: (defn selectors [] (fn[] [:div....])). That's it

Michael Nardell16:01:45

I am working on getting proficient at ClojureScript, as such interested in simple projects that can be developed in cljs. I am working on a very simple app that will emit XML for a recording scheduling system. I have tried to use the clojure.data.xml with ClojureScript and always running into problems. Of late I have been working in a Shadow-CLJS environment. I have it enumerated in my shadow-cljs.edn :dependancies, but whenever I try to add it in the apps ns :require I get an error. I had thought from reading the Readme.md for this project (https://github.com/clojure/data.xml) that I could use it in my ClojureScript work. Let me know if I am misunderstanding and if so any recommendations for a XML library that can be used in cljs.

alex04:01:02

What is the error you're getting? And can you copy + paste how you are requiring the library?

alex04:01:26

Based on my reading of that README, it does seem to support clojurescript

Michael Nardell17:01:59

Alex - sorry for not including the error. I think the problem was due to the clojure.data.xml dependency not loading; after cleaning up my code the error read something like "Not able to to find cljs for clojure.data.xml in the classpath" and it suggested that it may be a clj only library. After restarting the shadow-cljs watch and browser-repl processes I got the dependancies to load properly and clojure.data.xml now appears to be working in my Clojurescipt project. So I suppose that is my question - what is the best way to load/reload dependencies when working in Clojurescript, I am working in Shadow-CLJS right now but I have used Figwheel, and also followed the Clojurescript Quickstart guide. In all of these approaches the area where my understanding is blurriest is connected with dependancies (how and when they are reloaded). I suppose it is a pretty complicated story in cljs, since a dependency could be clj, cljs, or js. Any advice on how to be more comfortable with that concern would be helpfu.

Michael Nardell18:01:16

Retracing my steps, one of the errors that really confused me was this:

Invalid namespace declaration
-- Syntax error -------------------

  (... ... (:require
            [clojure.data.xml :refer :all]
has extra input
fixed this by changing to: [clojure.data.xml :as :xml]. I was still getting an error with this change, until I forced the dependancies to load.

Michael Nardell20:01:49

Bit more detail on working with clojure.data.xml in Clojurescript. It seems that some functions are available while others are not. For example emit-str works while indent-str does not - I get the following error: Use of undeclared Var clojure.data.xml/indent-str

alex03:01:46

Restarting the shadow-cljs watch process is the a surefire way to install missing dependencies. There are also tools which can add missing deps/libraries to your active REPL (see https://github.com/teknql/wing/blob/master/src/wing/repl.clj#L21-L39 as an example). I recall hearing that a new version of Clojure tools has this capability as well, but I'm not familiar with it I believe :refer :all is forbidden in CLJs. In general, it's rare that you need to actually require everything from the namespace and it may make your code harder to read (not knowing where functions are coming from). This is a personal preference though Your anecdote about only being able to use some data.xml functions in CLJs. This is probably why: https://github.com/clojure/data.xml/blob/5b13e2b2e8cb5445ac6c5f6926f3591b10eba66d/src/main/clojure/clojure/data/xml.cljs Note there's both a xml.clj and xml.cljs, and they contain different functions & implementations i.e. indent-str does not exist in xml.cljs

alex03:01:28

https://groups.google.com/g/clojurescript/c/SzYK08Oduxo/m/p0bWhM5lftUJ?pli=1 mentions some of the reasons behind disabling :refer :all in Cljs

Michael Nardell15:01:50

Thanks - I appreciate the guidance on how / where to look in the source code when these kinds of questions come up.