clojurescript

ClojureScript, a dialect of Clojure that compiles to JavaScript http://clojurescript.org | Currently at 1.10.879
bibiki 2021-01-16T15:45:48.035400Z

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]])

bibiki 2021-01-16T15:47:17.036800Z

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

bibiki 2021-01-16T15:47:43.037300Z

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

Michael Nardell 2021-01-16T16:41:45.042400Z

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.

Michael Nardell 2021-01-17T17:19:59.048600Z

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 Nardell 2021-01-17T18:40:16.051700Z

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 Nardell 2021-01-17T20:16:49.052600Z

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

Alex 2021-01-18T03:26:46.053100Z

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

Alex 2021-01-18T03:27:28.053500Z

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

Michael Nardell 2021-01-18T15:44:50.055600Z

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

bibiki 2021-01-16T17:54:58.042900Z

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