This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-10-11
Channels
- # beginners (57)
- # boot (9)
- # clara (9)
- # cljs-dev (12)
- # clojure (98)
- # clojure-boston (1)
- # clojure-dusseldorf (12)
- # clojure-france (1)
- # clojure-greece (1)
- # clojure-spec (41)
- # clojure-uk (86)
- # clojurescript (60)
- # code-art (2)
- # data-science (5)
- # datomic (5)
- # duct (1)
- # fulcro (14)
- # graphql (2)
- # lein-figwheel (4)
- # luminus (1)
- # midje (1)
- # off-topic (19)
- # om (10)
- # onyx (13)
- # pedestal (5)
- # portkey (59)
- # re-frame (31)
- # reagent (1)
- # ring (14)
- # ring-swagger (1)
- # rum (5)
- # shadow-cljs (90)
- # spacemacs (5)
- # specter (47)
- # sql (9)
- # uncomplicate (95)
- # vim (32)
Which one of these would be preferred in ClojureScript
;; capture the dom element in a variable at the top of the namespace
(def elements (.querySelectorAll js/document ".elementName"))
;; create a getter function
(defn get-elements []
(.querySelectorAll js/document ".elementName"))
The def
will only execute once, when that code is loaded. The defn
will execute the selector query each time you call it.
Seems like you'd want the function @tkjone?
I suppose it might depend on the situation. In general, I started by always using def
but then moved to defn
as it felt more idiomatic
working with some api wanna do this
curl --user api-key: -XPUT --data-binary @- << EOF
if (rows.length > 10) {
alarm('Too many rows');
}
EOF
{
"success": true
}
how do i do that with clojure?
i am using https://github.com/dakrone/clj-http
but i am unable to find how to do binary-data
only thing i found is byte-array in :mutipart
for POST
has anyone done this ?I'm sure we've done that... Hmm. I think we created a ByteArrayOutputStream and dumped the body there, and set the header correctly. It was a while ago though, so I'm not sure
i'm struggling a bit with core.async / channels. maybe someone can help? i have a collection of channels (representing web requests) and i want to take the first value from each channel and return the results in a collection. in other words, i want to wait for all channels to return a value before returning a final value. (this is cljs so i don't have access to <!!
)
(go (js/console.log "finished" (<! (a/merge [chan-a chan-b chan-c]))))
only returns the results from one of the channels
how does one recreate this
curl --user api-key: -XPUT --data-binary @- << EOF
if (rows.length > 10) {
alarm('Too many rows');
}
EOF
{
"success": true
}
using https://github.com/dakrone/clj-http#post
?Is that backwards? That looks like it'll do a GET and save to disk, whereas I think @lepistane was wanting to read data from stdin and PUT it. The principle is still valid though. Just flip it round and use *in*
instead of a file I guess?
ah, i see it's how java.lang.String.split works, hey ho. I will just nobble a "" on the end using my own function I guess
@bherrmann ahh, interesting approach, thanks!
I have a nested json data structure that isn't being decoded. Only the first level is being decoded and it leaves the maps coded as js json.
(let [sales-results (js->clj (.parse js/JSON (-> data :results :sales-results)) :keywordize-keys true)]
result:
[{"type":"SalesOrder","createdfrom":"","statusref":"pendingApproval"......
What have I done wrong?You’re not doing anything wrong, that’s just the way js->clj
is designed to work.
how do I make it recursive, I need the sub maps translated
Actually, I’m second-guessing myself now, I’m sure I read somewhere recently that js->clj
is not recursive, but I can’t find the reference now
it is just a multi level structure, it is a vector of maps snippet before decoding
"[{\"type\":\"SalesOrd\",\"createdfrom\":\"\",\"statusref\":\"pendingApproval\"
it claims to be recursive here https://cljs.github.io/api/cljs.core/js-GTclj
yeah I'm not having the same problem on the back end 😉 maybe I should do the translation on the backend
Ok, I found the source of my mistake: I was remembering the docs for #js
, not js->clj
.
So yeah, I’d say js->clj
should be recursive. What do you get with (.log js/console (.parse js/JSON ...))
?
sorry brb
Actually, your approach+data seems to work for me:
cljs.user=> (def j "[{\"type\":\"SalesOrd\",\"createdfrom\":\"\",\"statusref\":\"pendingApproval\"}]")
#'cljs.user/j
cljs.user=> (.parse js/JSON j)
#js [#js {:type "SalesOrd", :createdfrom "", :statusref "pendingApproval"}]
cljs.user=> (def p (.parse js/JSON j))
#'cljs.user/p
cljs.user=> p
#js [#js {:type "SalesOrd", :createdfrom "", :statusref "pendingApproval"}]
cljs.user=> (js->clj p :keywordize-keys true)
[{:type "SalesOrd", :createdfrom "", :statusref "pendingApproval"}]
cljs.user=>
sorry had to run my son to school
hmm wonder if I am using an old version of something, I inherited the project from a teammate.
@thaddeus.aid drive safely
@manutter51 yeah when I work directly with the repl it works fine, I wonder what the difference on the browser is.
dev:cljs.user=> (def j "[{\"type\":\"SalesOrd\",\"createdfrom\":\"\",\"statusref\":\"pendingApproval\"},{\"type\":\"SalesOrd\",\"createdfrom\":\"\",\"statusref\":\"pendingApproval\"}]")
#'cljs.user/j
dev:cljs.user=> (.parse js/JSON j)
#js [#js {:type "SalesOrd", :createdfrom "", :statusref "pendingApproval"} #js {:type "SalesOrd", :createdfrom "", :statusref "pendingApproval"}]
dev:cljs.user=> (def p (.parse js/JSON j))
#'cljs.user/p
dev:cljs.user=> p
#js [#js {:type "SalesOrd", :createdfrom "", :statusref "pendingApproval"} #js {:type "SalesOrd", :createdfrom "", :statusref "pendingApproval"}]
dev:cljs.user=> (js->clj p :keywordize-keys true)
[{:type "SalesOrd", :createdfrom "", :statusref "pendingApproval"}
{:type "SalesOrd", :createdfrom "", :statusref "pendingApproval"}]
dev:cljs.user=>
I’d say try replacing the js->clj
with .log js/console
and see what data structure the JSON parse is giving you, you might be getting back something unexpected
yeah isn't behaving as I would expect
(defn translate-sales-results [sales-results]
(let [j (.parse js/JSON sales-results)]
(let [p (js->clj j :keywordize-keys true)]
(println "#####translate: ")
(println p)
(.log js/console "#####translate!: ")
(.log js/console p))
))
REPL:
#####translate:
[{"type":"SalesOrd","createdfrom":"","statusref":"pendingApproval","tranid":"SO303","trandate":"10/10/2017"....
Safari console log
[Log] #####translate!: (ui.js, line 1274)
[{"type":"SalesOrd","createdfrom":"","statusref":"pendingApproval","tranid":"SO303","trandate":"10/10/2017"....
on Safari console
[Log] #####j: (ui.js, line 1278)
[{"type":"SalesOrd","createdfrom":"","statusref":"pendingApproval"...
not the
#js [ #js {...
that we would expectConsole won’t show you the #js
, that’s a cljs thingy and console is JS only
it would be interesting to compare that to (prn ...)
instead of (.log js/console ...)
(but harder to read IMO
it should show that on the REPL though shouldn't it?
transit is failing as well 😞
Transit -> println
[{"type":"SalesOrd","createdfrom":"","statusref":"pendingApproval"...
You’re doing this in safari?
yeah I was having some problems with my Chrome installation that I haven't fixed yet
huh, looks like it actually thinks the whole thing is still a string...
Kinda stumped, but maybe workaround with (clojure.walk/walk js->clj parsed-json-data)
as a temporary workaround
oh I think I have made progress.
oh wait, if it’s treating it all like a string still, yeah
Is there a clojure equivalent to Common Lisp shadowing-import
? That is, I want to refer a symbol from another namespace that will clobber something already in clojure.core
but I know what I am doing so I don't want clojure to warn about it.
Look at (:refer-clojure :exclude [sym1 sym2])
@mseddon
That will suppress the clojure.core
symbols so you can refer in your own.