Fork me on GitHub
#clojurescript
<
2018-03-01
>
wqhhust01:03:52

How to use ajax in dev mode? If I just use relative url, then it will actually get data from http://0.0.0.0:3449/some_relative_path. But I am using django as backend, and the port should be 5555 instead of 3449, so how to write url for ajax?

antonio01:03:43

do you tried to include the absolute URL?

wqhhust02:03:34

no, I don't

wqhhust02:03:42

just want to use relative url

wqhhust02:03:53

since I want to use django + cljs

wqhhust02:03:30

If use absolution url, then I need to put domain to the url, but the domain is based on whether it's prod or qa or dev.

justinlee02:03:18

@wqhhust I came to the conclusion that it’s not possible. You either need to rewrite the entire url or you need to proxy your backend. I may be wrong, of course.

antonio02:03:22

maybe you could just replace the port of the request URL to create the ajax and be happy

noisesmith02:03:27

@aeudes with a jsHREF object you can use set! on the .-port property

noisesmith02:03:40

and then put that HREF in your anchor

noisesmith02:03:56

sadly there's no literal for it, but some code can do it

wqhhust02:03:08

if you use clojure + clojuresript, the backend is clojure, the port is also NOT 3449, then how to call ajax even if using full stack clojure?

noisesmith02:03:28

oops, I tagged the wrong person above - so to change the port on a URL you need some js or cljs code

noisesmith02:03:57

or you can have the static absolute URL passed in (eg. if your html file is a template, the host address canbe injected as a var in a script tag)

wqhhust02:03:47

got it, thx

wqhhust02:03:04

Sorry, I am pretty new here. How to get the domain in clojurescript?

justinlee02:03:27

you know, maybe it is simpler than I thought: just use window.location.href

noisesmith02:03:34

you can create (js/URL. "//foo") that is your relative pathed url, now set the .-port property

noisesmith02:03:03

of course put the actual path you want in that string, once you set the port it's good to use

noisesmith02:03:27

it's cleaner than using regexes or storing the absolute path or whatever IMHO

justinlee02:03:11

in my case, i was using cljs-http which appears only to take a string

noisesmith02:03:58

justinlee: URL has a toString method of course

cmal02:03:16

Hi, what is the best way to do (a-to-fixed-func-but-return-number 1.11512 2) ;; => 1.12 in ClojureScript?

mfikes03:03:11

@cmal porting precisionRound from here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round

(defn precision-round [number precision]
  (let [factor (Math/pow 10 precision)]
    (/ (Math/round (* number factor)) factor)))
might match what you want

wqhhust05:03:01

Why the following code changed the url?

wqhhust05:03:15

I just want to create a new url

wqhhust05:03:46

(defn page [ratom]
 (let [a (.-location js/document)
       b (string/replace a  #":3449" ":5555")
       ]
   (js/alert b)
   [:div
    "Welcome to test reagent-figwheel."]
   )
 )

sbrg08:03:20

Hiya. I just tried starting a new project via leiningen with lein new re-frame cljs +cider +10x +garden +re-frisk +routes, and then running with lein figwheel dev. upon doing this, it installed a bunch of dependencies and the then it suddenly errors out with: https://gist.github.com/3e04ee1a13642437561cde6be5d6569d

sbrg08:03:34

i've also tried using three different versions of clojurescript. 1.9.{946, 908, 854}

sbrg08:03:05

never mind -- I found the answer. don't name your project cljs 🙂

benzap09:03:34

It's been a while since i've compiled clojurescript to :nodejs, are you able to use the usual clojurescript dependencies with a nodejs compilation target, or are you limited to using node_modules?

Empperi09:03:48

haven't used it but I would guess you should totally be able to use normal clojurescript dependencies, especially with :advanced optimizations which will embed all of your dependencies within your final js file

Empperi09:03:11

that being said, the usefulness of :advanced is rather limited with server environment

benzap09:03:18

Yeah, I figured

benzap09:03:46

well, I was thinking of developing a barebones template for creating commandline tools with nexe

benzap09:03:10

advanced compilation 'might' come in handy, but i'll likely target simple

roninhacker10:03:23

@benzap: something seems to be up with the normal requirement flow when targeting node: https://github.com/clojure/clojurescript/commit/53070a2c5392e8bdc00bb81c45034268e1cd81f2

benzap10:03:22

@dchristianbell so is the current version of clojurescript broken when pulling in npm builds with the :nodejs target?

roninhacker10:03:34

far as I can tell there are three (!) paths to pulling js libs in node projects

roninhacker10:03:18

1. cljsjs/, in your :dependencies. for node, my impression is that this is broken until next cljs release

roninhacker10:03:37

2. :npm-deps, which I think works but have not tried

roninhacker10:03:08

3. using the npm/yarn chain instead; npm install, etc

roninhacker10:03:17

As David said above, using node modules is still in sort of a wild west state

benzap10:03:19

interesting

roninhacker10:03:27

so I'm not quite sure enough about this for my comfort

benzap10:03:38

cljs.nodejs/require should still work though?

benzap10:03:52

I'm guessing npm-deps leverages that under the hood

benzap10:03:19

are they trying to get rid of the :nodejs target in later releases?

benzap10:03:30

he doesn't include a :nodejs target

benzap10:03:38

I'm in the process of testing it without the :nodejs target

sbrg12:03:27

Using react-router with JS/TS or some such, I can manually type in a url to a specific place in my SPA like and have that routing happen "client-side" so that the path bar/baz renders some specific component. How can I achieve the same behavior in clojurescript? I've been playing around with secretary + accountant, and I'm just not able to replicate this behavior. I'm essentially trying to do something like: user presses sign in -> user gets redirected to github for oauth flow -> user gets redirected back to SPA at route /auth_callback?secret=params&go=here -> SPA performs API call to backend with secret params to auth user and get a cookie I have implemented more or less exactly this flow using react-router in typescript. the crux is that react-router handles the /auth_callback route, while in clojurescript, the redirect just gives me a figwheel error about the resource not being found.

sbrg13:03:42

I figured it out. The issue was with figwheel. I needed to configure it to always serve up index.html instead of a 404. https://pez.github.io/2016/03/01/Reagent-clientside-routing-with-Bidi-and-Accountant.html

derpocious13:03:42

Hello, does anyone know how to add regular nodejs packages in cljs 1.8? I tried to add them to :npm { :dependencies }, but then why I try to rewrite it on my code I always get "namespace not found" :(

roninhacker13:03:54

Hey derp, are you targeting node? I think__ you may have to do it manually, writing a package.json and going through npm/yarn

mfikes13:03:21

@derpocious I'm curious, did you run lein npm install . (I've never used lein-npm, but just guessing.)

mfikes13:03:08

@derpocious It is also not clear whether lein-npm goes beyond just making the deps available. In other words, can you use the deps in a :require or do you need to treat them as foreign libs, or drop down to JavaScript interop to use them?

derpocious13:03:41

@dchristianbell yes I'm targeting node. I have a Node_modules folder, but how do I add it?

derpocious13:03:23

Thanks @mfikes, lein npm install doesn't seem to help

mfikes13:03:57

@derpocious If you are using :require then you need to tell ClojureScript about the foreign deps, if that's the way lein-npm works (just guessing again). In other words ClojureScript isn't going to automatically look in to the NPM deps tree

roninhacker13:03:51

I don't know if cljs.nodejs is in 1.8, but the source for its (node version of) require is dead simple:

roninhacker13:03:52

(def require (js* "require"))

mfikes13:03:54

Maybe lein-npm does all this for you. I can't really contribute having never used it. Sorry.

roninhacker13:03:15

something like ((js* "require") "module-name-here") might work.

roninhacker13:03:41

(and yes this is a hack, sorry)

derpocious01:03:15

just wondering, can I use :require with this in the same file to bring in my other cljs libraries as well?

andrea.crotti14:03:29

in theory should I be able to call a Clojurescript function from the JS console?

derpocious14:03:37

Ok thanks guys. And @dchristianbell when I do module name it should be without the double quotes right?

andrea.crotti14:03:41

(or well from Javascript in general)

roninhacker14:03:58

no, the double quotes are intentional

andrea.crotti14:03:05

I did some "interop" by passing some data with the window['config'] trick, but if I could call the functions directly it would be even much better

roninhacker14:03:40

@andrea.crotti you should be able to, but you'll be typing a bunch

andrea.crotti14:03:11

hehe ah yes I tried to auto complete around but could not get anywhere

roninhacker14:03:20

project.myproject.projectnamespacebeginning.projectnamespace.ohheywefinallygotafunction.call(null, args)

andrea.crotti14:03:05

hehe ok thanks

andrea.crotti14:03:25

probably better generate the actual output I want and stuff it into window.config still thene

derpocious14:03:02

what's the window.config trick? :)

roninhacker14:03:26

something like: (let [resulting-data (cljs-fns input-data)] (set! (.config js/window) resulting-data))

sggdfgf14:03:55

Is it possible to await for a promise from JavaScript?

roman01la14:03:42

(-> promise
   (.then #(console.log %))
   (.catch #(console.log %)))

sggdfgf14:03:33

@roman01la but it will return promise it self not a value... Let's say then lambda returns a number - and I want that

roman01la14:03:12

There’s no way to “unwrap” JS Promise

roman01la14:03:46

I usually use Deferred objects for that purpose

jannis14:03:56

@dnolen I think I found a bug in the node module indexing indeed. I think it reports package.json -> "module" -> "index.mjs" as index.js (which may not even exist) instead of index.mjs. Still investigating though with a help of a test in cljs.closure-tests.

jannis15:03:26

Although, this is what it generates for iterall, which has an index.mjs:

{:file
  "/Users/jannis/Work/personal/groom-npm-deps/node_modules/iterall/index.mjs",
  :module-type :es6,
  :provides ["iterall" "iterall/index.mjs" "iterall/index.mjs"]}
That looks ok, even if the last item could maybe be iterall/index instead (it does that if the main entry is index.js). However, the resulting cljs_deps.js only has:
goog.addDependency("../foo/core.js", ['foo.core'], ['cljs.core', 'iterall']);
and no additional lines for iterall. That part looks wrong.

andrea.crotti14:03:44

ah that's better than mine @dchristianbell I just did

client-side-config (json/write-str (assoc config
                                                  :language language))
;; and then
     [:body
      [:script (format "window['config']=%s" client-side-config)]

sggdfgf14:03:56

@roman01la just found your youtube channel 🙂 nice!

andrea.crotti14:03:47

but well I think it's not exactly the same thing, with "my trick" I can share stuff from the backend to the JS directly

anel15:03:09

guys, can someone help me to resolve an error that I’m getting please. I’m new to cljs, when I try to run my tests in repl with (cljs.test/run-tests), I’m getting #object[TypeError TypeError: undefined is not an object (evaluating ‘cljs.test.run_block’)] error

sggdfgf15:03:29

do you have source to share?

anel15:03:55

im not allowed to share the code 😞 I can give you the details of my setup, what would you need to know?

derpocious15:03:28

"undefined is not an object". You are trying to call a property or method of something that is undefined

derpocious15:03:46

Does it give you a line number?

sggdfgf15:03:15

@anelkudebayeva You want to check if you are accessing all the fields of an object that are defined.

sggdfgf15:03:47

maybe you are havving warnings that you are using undeclared vars on compile time

anel15:03:09

@faxa everything compiles with no errors or warnings, these tests also run without any issue in clojure repl, so I’m assuming it has something to do with cljs repl?

sggdfgf15:03:22

hard to tell

sggdfgf16:03:07

@anelkudebayeva in clojurescript as oposite clojure libs will be different

sggdfgf16:03:33

so you have Javascript environment instead of say JVM

anel16:03:48

umm well its clojurescript that im trying to test but those functions are essentially equivalent in clojure, so I ran a quick test in clojure and it worked

anel16:03:51

i can run the actually app no problem, it’s the unit tests that I can’t run at all

Garrett Hopper19:03:59

Is there a way for me to compile with advanced optimization and include node_modules in that output?

Garrett Hopper19:03:54

Or somehow specify a destination for node_modules? I'm building multiple cljs pieces, and I don't want them all sharing the same node_modules (which I'll have to copy with the output for it to work).

Garrett Hopper19:03:14

I'd rather not have every one of them required the node dependencies of every other piece.

tomc19:03:19

@ghopper If you're using closure modules I can't help you because I don't know much about that, but if you're defining separate builds you can use :foreign-libs for that.

Garrett Hopper19:03:55

Well, I'm using :npm-deps with :install-deps. I'm not familiar with :foreign-libs.

Garrett Hopper19:03:25

I'm using boot, so perhaps I could get it to run the cljs compilation from a different directory to make :install-deps install in a different directory?

tomc19:03:00

That seems plausible. My understanding is that npm-deps is basically shorthand for foreign libs, so using different builds w/ different npm deps might work.

Garrett Hopper19:03:27

The problem is I'm not sure how to get boot-cljs to compile from a different directory.

Garrett Hopper19:03:42

Interesting, I thought foreign-libs was completely different.

tomc19:03:24

I've got a problem where a foreign-lib is included twice in the output javascript. Example project here: https://github.com/tomconnors/problems/blob/reagent-preact/project.clj. preact-compat is included twice in the prd build's out. Is this a known issue?

Garrett Hopper19:03:29

So, are foreign-libs never advanced optimized?

quoll20:03:28

Would anyone know where ##Inf is declared please?

quoll20:03:22

it’s referred to in cljs.core, but I can’t see where it comes from (and it causes an error for me)

tomc20:03:32

@ghopper They're just prepended to the output file. Pretty sure they get :simple optimizations.

noisesmith20:03:00

@quoll it's a reader, and those errors typically mean something is using an older version of the clojurescript reader

quoll20:03:23

ah… the # should have clued me in. Thank you

quoll20:03:26

Updated versions of things, cleaned and recompiled, and it worked. Thanks @noisesmith

quoll20:03:28

I’d found that code, but hadn’t put it together what I was looking at

quoll20:03:41

I’m much more a Clojure person than Clojurescript. Still learning

noisesmith20:03:29

well - the thing I linked is clojure code, technically, heh

noisesmith20:03:37

it's just the clojure code that happens to turn cljs into js

noisesmith20:03:00

I'm sure bootstrapped cljs has the same thing from some other place

dnolen21:03:07

live discussion about the new cljs.main