Fork me on GitHub
#clojurescript
<
2017-11-18
>
chris_04:11:59

Can I ask a dumb question? For a cljs-only (no server, no real .clj files) re-frame app, I serve a myapp/resources/public/index.html with a body element like:

<body>
    <div id="app"></div>
    <script src="js/compiled/app.js"></script>
    <script>myapp.core.init();</script>
  </body>
But for a server + re-frame app built from a Luminus template, the same index.html file (when served from a Compojure route and rendered via Selmer) throws an error in the console: Uncaught ReferenceError: myapp is not defined at (index):30 Any pointers why this is happening?

chris_04:11:05

Many thanks

qqq04:11:23

in cljs, how do I check if an object is an atom ?

mikethompson04:11:08

@chris_ there is a race condition. app.js has to fully load before myapp.core.init() can be called.

mikethompson04:11:36

Best option is to change the script part to be:

<script>window.onload = function () { myapp.core.init(); }</script>

mikethompson04:11:05

Then init() will only be called once the app.js has loaded

mikethompson04:11:08

@chris_ I've filed a bug report: https://github.com/Day8/re-frame-template/issues/62 (I have no idea how this has not come up before now)

mikethompson05:11:47

@qqq

(defn atom?  [a]
   (satisfies? IAtom a))
or
(defn atom? [a]
   (instance? Atom a))
I'm guessing the former is better. But it depends.

cmal05:11:29

Hi, how could clojurescript know a value is the Javascript's undefined and NaN?

cmal05:11:44

for example, some API returns an array like [null, undefined, NaN, 3] , and in ClojureScript we want to check whether some value in an array is null, undefined, or NaN.

mikethompson05:11:40

Eg: (undefined? js/window.bogus)

mikethompson05:11:56

Also (== something js/NaN)

mikethompson05:11:22

But you have the idea: js/NaN

cmal05:11:16

I just made a mistake: it seems the API cannot return undefined or NaN because JSON does not allowed these.

mikethompson05:11:49

(js/isNaN js/NaN) should return true

cmal05:11:08

Thanks to you @mikethompson I know how to use undefined? and js/isNan and js/NaN.

cmal05:11:54

Btw, @mikethompson, do you know any posts or books describe javascript interop in detail?

cmal05:11:13

I've never see in anywhere there is a undefined? function.

cmal09:11:53

@ajs Thank you very much

mfikes14:11:26

FWIW, misuse of aget was fixed in the core.async 0.3.465 release yesterday, so one less thing preventing making use of the :checked-arrays feature https://clojurescript.org/reference/compiler-options#checked-arrays

mfikes14:11:02

I'm also happy that, with that release, you can now write (require '[clojure.core.async :refer [chan <! >! go]]) and it will "just work". (No more differences between ClojureScript and Clojure for your ns forms and core.async, effectively!)

juhoteperi17:11:28

@mfikes Any tips what would be proper way to solve array length check failure? Assert failed: (< idx (alength array)) I think this is caused by Reagent using an array so that it automatically grows based on the largest used index.

mfikes17:11:13

@juhoteperi It is unfortunate that the assert doesn't include the values involved...

juhoteperi17:11:02

I think in this case, array starts as empty but it should grow when new values are added

mfikes17:11:40

Ahh, right, relying on JavaScript host behavior

mfikes17:11:11

The asserts are not written to allow that. A change in the compiler would be needed.

mfikes17:11:30

As an aside, it would be interesting to have an official spec for aset, which would then be another way to determine what arguments are legal.

zakpatterson17:11:17

Hey, we're trying to figure out how to make reusable reagent components that don't break figwheel in dev, e.g.:

(defn component [& args]
    (let [state (r/atom {})]      ;; this clears state on every save in dev.
        (fn [& args]  ;;; uses state in here, for stuff like form values etc.
vs
(defonce component-state (r/atom {}))    ;; now my component isn't reusable because all instances will share the same state.
(defn component [& args]
    (let [state component-state]
        (fn [& args]  

zakpatterson17:11:22

i'll try posting this in #reagent too, was just considering if there's a clever macro solution that would help here

noisesmith17:11:24

@zakpatterson for not breaking figwheel, why not (defn component [state & args] ....) (defonce a-state (r/atom nil)) (defonce b-state (r/atom nil)) [:div (component a-state) (component b-state)]

zakpatterson17:11:56

hm i suppose that's good - it adds an extra thing the user has to be aware of, but i suppose i could just add a variable arity constructor too

noisesmith17:11:10

I can't think of a version that would be transparent to figwheel without needing seperate tracking of the state outside the component though

noisesmith17:11:36

they can decide how that atom comes in, and yeah you can make a convenience function if they don't care about figwheel behavior

sova-soars-the-sora18:11:15

favorite clojurescript IDEs?

chris_21:11:02

@mikethompson Ah, I see; thank you very much! 🙂

chris_22:11:55

Hmmm… Perhaps I’m misunderstanding @mikethompson? The following HTML body also produces an uncaught reference: Uncaught ReferenceError: myapp is not defined at window.onload

<body>
    <div id="app"></div>
    <script src="js/compiled/app.js"></script>
    <script>window.onload = function () { myapp.core.init(); }</script>
  </body>

thheller22:11:09

@chris_ is this with :advanced? maybe you missed the ^:export meta on init?

mikethompson23:11:03

@chris_ Sorry, but it looks like I mislead you. @thheller has taken the time to put me straight https://github.com/Day8/re-frame-template/issues/62#issuecomment-345475898

mikethompson23:11:08

In which case, the problem has to be either: 1. an :advanced build without ^:export on init (but if you are using the re-frame template this should be handled https://github.com/Day8/re-frame-template/blob/master/src/leiningen/new/re_frame/src/cljs/core.cljs#L20) 2. maybe the namespace for your init is not really myapp.core (quite literally check the ns at the top of where init is defn-ed)