Fork me on GitHub
#clojurescript
<
2021-10-12
>
Muhammad Hamza Chippa10:10:21

I am having hard time understanding how re-frame works, is there any book or good documentation available for it ?

p-himik10:10:18

Have you read the official documentation?

borkdude10:10:37

There is also a Learn Re-frame course online by @U8A5NMMGD

Muhammad Hamza Chippa11:10:02

yeah I did read them

p-himik11:10:45

Finally, if you have some specific trouble points, just ask away.

Muhammad Hamza Chippa11:10:49

I have three task to do but first I must try on my own , if I still fail to do it . I I will surely trouble you

p-himik11:10:13

Of course. I didn't mean "ask how to do your job". :) I meant more like "if you don't understand the concept X in re-frame, ask about that concept".

Muhammad Hamza Chippa11:10:47

ofcourse I will ask the concepts only not how to do my job kindda unethical

Muhammad Hamza Chippa11:10:57

re-frame.core/reg-event-fx is for defining the handlers for the event , and rf/dsipatch is for associating the event with the handler ?

p-himik11:10:19

In your understanding, what is the difference between "define a handler for an event" and "associate an event with a handler"?

Muhammad Hamza Chippa11:10:13

(rf/dispatch [:buy item-id])
here we are calling the event handler associated with a keyword buy

Muhammad Hamza Chippa11:10:33

then we define a handler for :buy keyword like this

(rf/reg-event-fx ;; register an event handler
  :buy           ;; for events with this name
  (fn [cofx [_ item-id]] ;; get the co-effects and destructure the event
    {:http-xhrio {:uri (str "" item-id "/purchase")
                  :method :post
                  :timeout 10000
                  :response-format (ajax/json-response-format {:keywords? true})
                  :on-success [:added-cart]
                  :on-failure [:notified-error]}
     :db (update-in (:db cofx) [:cart :items] conj {:item item-id})}))
\

p-himik11:10:01

Right, but "calling" is very different from "associating" - the word that you used initially. We have to be precise about the terminology to make sure that there's understanding. In either case, that's not really how it works. Re-frame has an event queue and an event handler registry. When you call any reg-event-* function, you associate an event ID with an event handler, and that association happens via that event handler registry. When you call dispatch, you add an event to the event queue. When re-frame has a chance (which it doesn't always have, given how JS is single-threaded), it goes through the whole queue, and for each event ID checks the event handler registry for the right handler, and calls it. The result of that handler is that interpreted based on what kind of function you used to register the handler. So when you call dispatch, none of your handler code is executed - you just add a "note" for re-frame to run that code when it gets the chance to check its "todo list" (the queue).

Muhammad Hamza Chippa11:10:23

That is helpful . get a little bit of idea what it is doing

p-himik11:10:36

What I described above is basically the first picture on this page: https://day8.github.io/re-frame/event-handling-infographic/

🙂 1
Muhammad Hamza Chippa12:10:06

that was very helpful cleared all the concepts , I was missing such article

Muhammad Hamza Chippa12:10:22

(defn pong "Implementation of the /api/ping endpoint." [_request] (response/ok {:ping "pong"}))

Muhammad Hamza Chippa12:10:13

I wanted to replace the {:ping "pong} with {:ping @value} where value is an item is there any way that I could send the value as the param to the API ?

p-himik13:10:16

Yes, in the request, although I have no clue what you're using on your backend and this is absolutely not a re-frame question.

Muhammad Hamza Chippa13:10:00

yes this is not a reframe question

Muhammad Hamza Chippa15:10:13

how to convert it into the form of [:ping "sasds"]

p-himik15:10:04

Please do not reuse threads for unrelated topics. And don't cross-post - there's already a discussion in #clojure

Muhammad Hamza Chippa21:10:36

is there any way to use database in cljs file , jdbc is not compatible with clj file

p-himik21:10:04

If you mean from a browser then no. If you mean from NodeJS, then yes - there are NodeJS libraries for that.

Muhammad Hamza Chippa21:10:39

how can I integrate with the clojurescript code any example out there ?

p-himik21:10:00

Read about ClojureScript/JavaScript interop.

Muhammad Hamza Chippa21:10:46

so I have to use javascript code and turn it into clojurescript syntax using interop ?

p-himik21:10:16

Yep. Or you can write JavaScript code and call it from CLJS via interop.

Muhammad Hamza Chippa21:10:54

okay that will be helpful

borkdude22:10:03

Note that when you use regular CLJS you probably have to leave out $default in the requires, since this is imposed by ES6 in nbb and regular CLJS compiled to CommonJS

Muhammad Hamza Chippa20:10:15

I have a .sql file which contains this code            create table if not exists users (
    id integer primary key,
    first_name text not null
);

insert into users (first_name) values
  ('Jerry'),
  ('Jenny'),
  ('George'),
  ('Johanna'),
  ('John'),
  ('Anne');
is this possible to import that file in clojure ?

p-himik20:10:08

@U02DQ45FQF9 Once again - please do not reuse threads for unrelated topics. Not only the topic of this thread is unrelated, the main channel is also completely unrelated. And, as well, please do not cross-post. I have already answered your question in #clojure

tessocog23:10:55

what is this UI theme called? Is there a template for this? https://defillama.com/home