Fork me on GitHub
#re-frame
<
2016-06-12
>
pauldelany11:06:24

@danielcompton: hey Daniel - actually, cljs-devtools is already installed, but perhaps the output looks different to what you expect due to the additional configuration - I blindly followed the instructions here regarding clairvoyant and re-frame-tracer: https://github.com/Day8/re-frame/wiki/Debugging

pauldelany11:06:39

cljs-devtools not fully configured! (thanks) updated console screengrab

pauldelany11:06:03

that second call to get-vote-by-id was from the repl

rorydouglas15:06:06

Perhaps a silly question, but can I (subscribe) an arbitrary side-effecting function (ie that's not a reagent component) to an event?

rorydouglas15:06:13

Sorry not an event

rorydouglas15:06:33

Have an arbitrary function called each time a subscription changes, is what I meant

rorydouglas16:06:15

something like

(let [sel (subscribe [:selection-changed])]
  (println “Selection is:” @sel))

rorydouglas16:06:48

also tried

(let [sel (subscribe [:selection-changed])]
  (add-watch sel :key (fn [ref key old new] (println "Selection is:" new))))

rorydouglas16:06:31

neither of those work

escherize16:06:30

you can put the function inside your subscription function

wasser16:06:37

What function does :selection-changed register to? They may be better ways depending.

escherize16:06:30

(register-sub :loading?
  (fn loading? [db _]
    (my-function-that-does-what-i-want @db)
    (reaction (:loading? @db))))

rorydouglas16:06:01

@wasser: :selection-changed is a subscription i setup with register-sub

wasser16:06:09

This looks like you want to kick off a (long-running) asynchronous task and then have your subscriptions re-render when it finishes. If that's true, the wiki has a good discussion of a workable approach: https://github.com/Day8/re-frame/wiki/Talking-To-Servers

rorydouglas16:06:22

@escherize’s approach would probably work for me

rorydouglas16:06:49

though it’s then fixed for teh life of the subscription

rorydouglas16:06:57

ideally i’d want something like add-watch

rorydouglas16:06:01

that i can add & remove at will

rorydouglas16:06:10

@wasser: i don’t have a specific use-case ironed out

rorydouglas16:06:24

just trying to figure out how to have arbitrary code trigger in response to a change in subscription

escherize16:06:41

I'd suggest my snippet then :]

rorydouglas16:06:06

i guess i can just add/remove the entire subscription, & optionally set the listener function when i need it

escherize16:06:25

if it's for intraspection, you should checkout the debugging wiki

escherize16:06:31

changed my life today

rorydouglas16:06:30

it started out as a debugging exercise 🙂

rorydouglas16:06:39

but it got me wondering more generally

rorydouglas16:06:47

shouldn’t i expect add-watch to work?

rorydouglas16:06:57

(subscribe) returns a ratom, which implements IWatchable

escherize16:06:59

add-watch works on the re-frame.db/app-db

escherize16:06:03

on the ratom

escherize16:06:17

I'm not really sure 🙂

rorydouglas16:06:23

or maybe it doesn’t return a ratom, maybe only a reaction

escherize16:06:35

I think it's a reaction - yea

rorydouglas16:06:17

but Reaction implements IWatchable too

rorydouglas16:06:37

maybe i need this :auto-run business

escherize16:06:57

Yeah.. it'd appear that you can't add-watch on a reaction.

rorydouglas17:06:04

aah figured (it/something) out

rorydouglas17:06:31

(let [sel (subscribe [:selection])]
    (run!
      (println “Selection:" @sel))))

rorydouglas17:06:35

does what i want

rorydouglas17:06:57

though, i don’t think i can de-register it

rorydouglas17:06:55

ah you can, if you require dispose! from reagent.ratom

rorydouglas17:06:02

(defn listener []
  (let [sel (subscribe [:selection])
        state (atom {:count 0})]
    (swap! state
          assoc :listener
          (run!
            (println "Sselection:" @sel)
            (swap! state update :count inc)
            (when (< 3 (:count @state))
              (dispose! (:listener @state)))))))

rorydouglas17:06:17

so that will listen for 3 changes then de-reg itself

danielcompton21:06:08

@pauldelany: in this code block

[:div
            [:div (str "cv: " @cv)]
            [:div (str "Current Vote id: " @current-vote-id)]]
is @current-vote-id returning anything?

pauldelany21:06:52

@danielcompton: yep - that's evaluating correctly (to 1, in the instance shown in the screengrab)

danielcompton21:06:16

what happens if you take out the filterv in get-vote-by-id, and just return all the votes?

danielcompton21:06:29

do they all show up in the div?

danielcompton22:06:32

The root cause of ^^ was a string was being set based on a cb from a secretary route handler.

rui.yang23:06:08

hi, wonder what is the best way of doing forms in reframe? mainly how to update model when ui changes