Fork me on GitHub
#clojurescript
<
2019-11-12
>
clarice08:11:26

Hello, can anyone help me with opening a new window using ClojureScript? I am new to JavaScript interop and I've created a question on SO regarding the problem I face with the solutions I have tried https://stackoverflow.com/questions/58814179/how-do-i-open-a-new-window-using-clojurescript I made it work once and can't seem to make it work again. 😞 Let me know if I need to provide any more details.

etiago08:11:54

Hey, I think you need something like this:

(.hello js/window) ;; JS output: window.hello();
I'm no CLJS expert, but the interop seems to work the same way as the Java one. So (.methodYouWantToCall objectYouWantToCallItOn) Source: https://www.spacjer.com/blog/2014/09/12/clojurescript-javascript-interop/

clarice09:11:40

Thanks! Yeah, the syntax was right to being with. When I started trying with set! I overrode the open function with a string :woman-facepalming: Silly mistake.

dabrazhe15:11:24

I am trying to call an http endpoint in a Clojurescript code targeting node.js (not browser). It is supposed to be a basic thing, but I haven't found an easy way to do it, nor libraries to use. What is the idiomatic way to make a http request in a node.js backe end cljs code?

Jimmy Miller16:11:24

@dennisa You can basically do it all the same way you would with just vanilla node. So it depends on what you are wanting from the api. If you don't want to pull in anything, just use the node https library. If you want something higher level, you can use something like axios or node-fetch.

metehan16:11:52

This is a working sample of my code. Works properly as I expect when I call this function from browser console but when I call same function with same data from another CLJS file It gives error.

Uncaught TypeError: Cannot read property 'push' of undefined
    at Object.spill$app$sockets$push [as push] (sockets.cljs:59)

metehan16:11:54

I am trying to solve this problem since morning I have no idea why it's different from browser console or from cljs.

Jimmy Miller16:11:46

What creates the channel? Are you sure it exists when that function is run? (I have no context. so not sure if get makes the channel itself)

thheller16:11:24

if I had to guess you are missing a desctructure

👍 4
metehan16:11:48

(defn channel [channel_name]
  (go/get (.-channels js/window) channel_name))
this how I create

thheller16:11:52

the :socket-push probabnly misses (fn [[channel event body]] ...)

👍 4
metehan16:11:50

ok i'll try to figure out thank you very much

metehan17:11:29

thank you very much it works 🙂🍺

atticmaverick18:11:31

is there a way to run multiple simultaneous figwheels? I have two cljsbuilds that I would like to run at the same time instead of having to (switch-to-build :build-name)

aisamu19:11:28

This might be a job for :extra-mains

Daniel Keane19:11:17

I assume you’re using lein-figwheel. You can run both builds by using: (start-figwheel! :build1 :build2) or do the same from the command line with: lein figwheel build1 build2

atticmaverick20:11:54

Thank you. that seems to work.

Aleks Abla20:11:47

Hi everyone! Hope you are all having a great day. I am a novice programmer, who recently picked up Clojure. It's been a lot of fun, and I have started playing around with ClojureScript and Re-Frame. I am having trouble wrapping my head around the interplay of routers, data stores, views, handlers and subscriptions. I understand what each one does theoretically, but I am shaky on how each interacts, especially as the app listens to the user and adapts. Could anyone recommend good resources to for me to look at? I am trying to build a single page application, that would act as a dashboard with basic statistics. Further down the line would love to add graphs and whatnot. these are what I have researched so far: https://github.com/day8/re-frame http://www.multunus.com/blog/2016/02/noobs-walkthrough-re-frame-app/

metehan20:11:55

this is very good source i learnt mostly from here

metehan20:11:49

re-frame course was expensive but I read this source commit by commit read all the changes in every commit and see what it produced -> https://github.com/jacekschae/learn-re-frame-course-files

Aleks Abla20:11:44

Awesome, I checked out the trial of the course. Will read through the changes, thanks!

metehan20:11:15

Actually I can say I learn't most of without even finishing every commit. Btw clojure community most helpful friendly community I2ve ever seen in my life ask anything in beginner channel everyone is eager to help 🙂

dabrazhe22:11:37

@jimmy I found some code node request library, but it returns #object[Request [object Object]] which I don't know how to parse easily. Have you or some has a code example of using node https libraryin cljs?

Jimmy Miller05:11:26

The https node library is a little complicated. But here is an example of node-fetch.

(ns my-app.core
  (:require ["node-fetch" :as fetch]))

(-> (fetch "")
    (.then (fn [res] (.json res)))
    (.then (fn [json] (println json))))

dabrazhe20:11:34

Thanks! I tried this before as well. What I am getting back is a #<Promise[~]> and no matter how I apply .then to it it's still returning a promise back, not the value itself.

Jimmy Miller20:11:39

Yes, that is how promises work. You cannot get out the value itself.

Jimmy Miller20:11:21

You use the then to get at the value and do something with it. Assign it an atom, send a request, etc. This is part of js being asynchronous. It is a weird thing to get used to.

dabrazhe20:11:13

thanks, though i am confused. I just need the json returned from the api endpoint. now whatever I do I get the promise. how can i get it in a map {}? There surely should be a way to get the value, how else can I operate on a data structure?

Jimmy Miller02:11:43

You call then on it. The function you pass to then will get handed the data. Above instead of printing you could call js->clj to get it into a map. You only gen the value in the then. That is just how these things work. This is just the nature of JavaScript.

Jimmy Miller02:11:39

I’d really recommend taking some time to learn JavaScript and how its asynchronous model works first. It is a big learning curve. But it makes sense after you do it a lot.

dabrazhe15:11:31

I guess I've found the issue: the code above won't print anything, nor the request body, so it appears that it only returns a promise, but does nothing else. The thing is, the print function is never called. The question is why it's not printing, ie calling the callback func? Can it be because of my setup? I am using shadow-cljswith node repl,

dabrazhe15:11:36

I can curl the url alright, with response time of 0.14 ms

Jimmy Miller17:11:01

npx create-cljs-project node-fetch-app
cd node-fetch-app
npm install node-fetch
npx shadow-cljs node-repl
Wait for that to load. Then type the following code:
(require '["node-fetch" :as fetch])

(-> (fetch "")
    (.then (fn [res] (.json res)))
    (.then (fn [json] (println json))))
I just did that and it printed out the response. If it doesn’t for you, there is something wrong with your environment.

dabrazhe11:11:53

alright, thanks a lot for writing this up! I guess it's my setup, i've restarted shadow-cljs server and repls; sometimes it works, sometimes it does not..

Jimmy Miller16:11:28

You can use (.catch (fn [error] (println error))) to see if there is a error being returned

dabrazhe19:11:29

Thank you jimmy, you've been of great help