https://github.com/djblue/portal might also be a solution
I am walking my first babysteps with Clojurescript and have a little issue adapting. For a start, I wanted to write a small frontend for the Hackernews API. The API offers a method to retrieve the IDs of the latest posts, afterwards I can fetch each post with a separate API Call. My current solution looks like this:
I don't see anything wrong with the code. Just a few notes:
- You're using the same name response
for different things in different functions
- post
and id
are mixed, which makes it a bit harder to understand (i.e. first-posts
is actually first-posts-ids
)
- go
uses a thread pool that's limited to 8 threads. Using it for IO may lead to unpleasant consequences in some cases
Regarding the last point: http://danboykis.com/posts/things-i-wish-i-knew-about-core-async/
Or I guess I might be wrong about it - after all, http/get
is itself async. I'm not that proficient with clojure.core.async
. :)
I believe http/get
is using core.async
, I was suprised by this, too, at first, but it makes sense. Thanks for your remarks, you are right about the namings!
And the linked blog is bookmarked for lunch, thx π
This feels clunky for me, but I am not able to put my finger on it. I am using Rum for the components, so the list of posts reacts on the state change in the atom. The application works, but I am pretty sure that this could be done better.
short version: Where should I add namespaces to keywords in API response maps in cljs SPA? And should I do that in the first place or just use unnaspaced keywords (and forget about clojure spec on frontend)?
longer version:
I'm trying out spec in my small cljs reagent app. I stumbled on a problem that API responses gotten via cljs-ajax
doesn't namespace keywords in maps ( https://github.com/JulianBirch/cljs-ajax/blob/master/src/ajax/json.cljc#L33 )
As I understand there's no elegant way to redefine behavior of read-json-native
in cljs-ajax (cljs-http too). And the only way to do this is to add namespaces in my app code with something like that:
(map->nsmap {:a 1 :b 2} (create-ns 'my.new.ns))
=> #:my.new.ns{:a 1, :b 2}
;; <https://stackoverflow.com/a/43722784/1856086>
Also I'm considering switch to httpurr
, as it gives more control on decoding function.
But maybe I'm missing something here? What's your experience of namespaced keywords's usage in cljs apps?@jehaby I am using cljs-ajax
through http-xhrio
from re-frame and namespaced keywords worked almost out of the box with these options:
{:http-xhrio {:method :get
:response-format (http/json-response-format {:keywords? true})
:uri <uri>
:on-failure [:common/set-error]}})
thanks! I'll take a look.
The solution above doesn't add namespaces by itself. It creates namespaced keywords iff the response has keys like "a/b"
. I.e. keys that would create namespaced keywords if passed to keyword
.
{:keywords? true}
just translates every key with keyword
, that's it.
@jehaby If you want to add your own namespaces, you will have to either transform the received data in your code directly or write your own response handler - just like read-json-native
, only with an extra :namespace
option or something like that.
Thank you!
Is there a quick and dirty way to see how ClojureScript would compile to JavaScript a regular function without having to set up a full project?
Oh fantastic!
@p-himik cljs.core.PersistentVector
do you know where are these structures defined in JavaScript?
@vincenz.chianese https://github.com/clojure/clojurescript/blob/599cd05fd271b4ce672e8a6124f0f785b1b3b2d0/src/main/cljs/cljs/core.cljs#L5505
but that ain't javascript; where are the collections effectively implemented in JavaScript @victorbjelkholm429?
Impossible to tell, depends on many factors. You will have to compile a small CLJS file that uses PersistentVector
and see what the code is.
That's what I did, and I can't find it. Example:
What's the example? :)
Hey guys, I'm having a weird problem here and lacking the Node.js-knowledge to solve it, When revisiting a project after a pause of about 3 weeks I was surprised that suddenly shadow-cljs tells me a dependency is missing:
The required JS dependency "querystring" is not available, it was required by "node_modules/url/url.js".
The module is installed under node_modules
however, and npm install --save url
succeeds but doesn't change a anything.
I might also mention that at first shadow-cljs complained about a missing dependency needed for nodemailer
(which is not even used by the frontend part of my app that I'm trying to compile here, but only by the backend which worked just fine) Any ideas on what to do next?
@services npm install shadow-cljs
in your project.
Thank you so much⦠this probably saved me a lot of time!
Also: Thanks for creating and maintaining shadow-cljs!
Hmm ok understood. I was wondering how these data structures were implemented in JavaScript. All right, thanks!