Fork me on GitHub
#re-frame
<
2016-10-24
>
rui.yang04:10:15

hi, wonder if it is possibe for a handler to listen changes on app db?

Pablo Fernandez10:10:04

I just released version 0.4.0 of my form building library: https://clojars.org/com.pupeno/free-form/versions/0.4.0 tested with re-frame 0.8.0 🙂

rui.yang10:10:21

looks like not many people actively looking at this channel atm

rui.yang10:10:01

in my app state, there are two part of the data, one change should trigger the other part to change, is there a way to do it or am I modeling sth wrong?

mccraigmccraig10:10:59

@rui.yang you can use an :after interceptor to 'listen' for changes to app-db https://github.com/Day8/re-frame/blob/master/docs/Interceptors.md#the-links-of-the-chain, or you can dispatch another event from your event handlers

rui.yang10:10:48

@mccraigmccraig thanks. I havent studied 8.0’s doc. had a look at inceptor, still dont know how to solve my scenario nicely

rui.yang10:10:55

my scenario is like: in UI: a client list drop down -> a list of client delivery addresses drop down -> a form with address details

rui.yang11:10:57

a change in client list drop down will set selected_client_id in app-db, which will reset address list drop down

rui.yang11:10:38

a change in address list drop down will set selected_address_id in app-db

rui.yang11:10:57

ideally, once selected_address_id change, a function should re-calculate the according fields of 'address form’ data in app db

mccraigmccraig11:10:00

@rui.yang which bit is causing you problems - sounds like wherever you have used the word 'change' above you should have an event handler, maybe with further event-handlers to deal with responses to async api calls if the UI is a client for a remote API ?

rui.yang11:10:11

@mccraigmccraig I guess I’ll have to dispatch two events on every change event above. eg. for changing address, I will dispatch an event to update selected_address_id, and a second event to reset ‘address form’ data in app-db

mccraigmccraig11:10:36

@rui.yang if you have the address form data readily available you can update both the selected_address_id and address form data in app-db in a single event handler, otherwise you may have to make an api call and dispatch on receipt of the response

rui.yang11:10:31

I have all the address data available in app-db. the problem is that I have built an unified way to handle general form change to app-db, that’s an :update-model event, triggered by on-blur of every form element. so I’ll have to dispatch another event to reset address details related field in app db

mccraigmccraig11:10:40

hmm - could :update-model be split into an event-handler which calls a db-update fn (which does all the work), and then you can re-use the fn in the event-handler which handles a :selected-address-id-change event ?

rui.yang11:10:31

handler for :update-model is pretty generic. it accepts a keys to locate part which needs to be updated in the app-db map, and a value. all it does is to set the value in app-db. I am not sure if I understand how to re-use

mccraigmccraig11:10:27

more abstractly, i'm suggesting that it will be easier if your re-frame events relate to things which are happening in your UI, rather than things you would like to do to your view-model, and to use regular fns to abstract mutations on your view-model - event-handlers are then free to re-use and mix view-model mutation fns as required

rui.yang11:10:06

@mccraigmccraig thanks for the tips.

curlyfry15:10:12

With the new reg-sub syntax (`:<- [:some-subscription]`) https://github.com/Day8/re-frame/blob/master/examples/todomvc/src/todomvc/subs.cljs#L130-L147, is there any way of getting the parameter of a subscription?

shaun-mahood15:10:58

@curlyfry: I've not tried it, but if you look at https://github.com/Day8/re-frame/blob/master/src/re_frame/subs.cljc#L97-L127 that syntax is just sugar on top of the 2nd variation. Maybe try writing your subscription as the 2nd variation, and then if you attach re-frisk you can take a look at the q-vec and d-vec that you are passing in and see how you would need to destructure them to pass them in to to your subscribe calls. If you run into a dead-end let me know and I might have some time today to look into it.

curlyfry15:10:58

@shaun-mahood: Cool, just tried using the 2nd version but totally missed that i get the d-vec in the second fn!

shaun-mahood16:10:23

@curlyfry: Does it all work then?

shyambalu16:10:51

in re-frame when you create a form-3 component

shyambalu16:10:05

is there a way to get access to the domnode it self

shyambalu16:10:17

to do some js inter-op on it

curlyfry16:10:36

@shaun-mahood: Not yet, trying to figure out exactly how the q-vec and d-vec work!

shyambalu16:10:10

because something things can’t be set as properties of a component such as focus and indeterminate

curlyfry16:10:59

@shaun-mahood: Is there any documentation anywhere on what q-vec and d-vec contain?

shaun-mahood16:10:26

@curlyfry: No docs that I know of, subscriptions are one of the areas where the docs are still pretty lacking since 0.8 unfortunately. What you could do to find out - add re-frisk to your project, and in the code for your subscription put in something like (re-frisk/add-data :sub-testing {:q-vec q-vec :d-vec d-vec}) and then pass different values in to see how what goes where. Alternatively you should be able to do the same with print statements.

curlyfry16:10:36

or just log them, right? 🙂

shaun-mahood16:10:55

@curlyfry: Have you looked into dynamic subscriptions at all? I haven't yet, but I expect that's what the d-vec is for based on the comments in the code.

curlyfry16:10:59

Oh, now I see you said that!

shaun-mahood16:10:01

Yeah, that works too 🙂

mccraigmccraig16:10:08

@shyambalu :component-did-mount (fn [ref] (reagent/dom-node ref))

shyambalu16:10:09

yeah that doesn’t seem to work

mccraigmccraig16:10:42

i'm using it plenty on desktop and mobile browsers @shyambalu - it definitely works (reagent 0.6.0)

shyambalu16:10:11

my bad you are riht

curlyfry16:10:20

@mikethompson I think I'm gonna need your expertise in figuring out how q-vec and d-vec work in subscriptions (if/when you have the time)! Slightly confused now, especially about when using a subscription that takes parameters.

mikethompson19:10:02

Oh, right, reading back in the stream

mikethompson19:10:21

So this doesn't work:

(reg-sub
	:derived-from-thing
	:<- [:thing here-i-want-an-id]       ;; <--  can't add  here-i-want-an-id 
	(fn [thing [_ the-same-id]]        
		 (derive-value-from thing the-same-id))
The :<-is meant only for the simple case that you don't need query params So when you do have them, you need to drop the use of the sugar and go back to the long form way ...
(reg-sub
	:derived-from-thing
    (fn [[_ id] _]                  ;; supply signal function - destructure q-vec to obtain id 
        (subscribe [:thing id]))      ;; now you can supply id to further subscription  
	(fn [thing [_ the-same-id]]
		 (derive-value-from thing the-same-id))

mikethompson19:10:41

I'm not sure you need d-vars. In fact, given this proclamation https://github.com/Day8/re-frame/issues/218 I'm not sure that anyone ever needs d-vars again

mikethompson19:10:40

Some explanation around d-vars here: in corner case #1 here: https://github.com/Day8/re-frame/issues/218#issuecomment-252470445

mattly19:10:44

has anyone done a write-up / how-to for getting re-frame and devcards playing nice with each other?