Fork me on GitHub

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: tested with re-frame 0.8.0 🙂


looks like not many people actively looking at this channel atm


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?


@rui.yang you can use an :after interceptor to 'listen' for changes to app-db, or you can dispatch another event from your event handlers


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


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


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


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


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


@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 ?


@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


@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


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


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 ?


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


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


@mccraigmccraig thanks for the tips.


With the new reg-sub syntax (`:<- [:some-subscription]`), is there any way of getting the parameter of a subscription?


@curlyfry: I've not tried it, but if you look at 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.


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


@curlyfry: Does it all work then?


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


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


to do some js inter-op on it


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


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


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


@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.


or just log them, right? 🙂


@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.


Oh, now I see you said that!


Yeah, that works too 🙂


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


yeah that doesn’t seem to work


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


my bad you are riht


@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.


Oh, right, reading back in the stream


So this doesn't work:

	:<- [: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 ...
    (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))


I'm not sure you need d-vars. In fact, given this proclamation I'm not sure that anyone ever needs d-vars again


Some explanation around d-vars here: in corner case #1 here:


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