Fork me on GitHub
#re-frame
<
2016-03-03
>
hjrnunes10:03:44

When I pass the subscription ratom to reagent-forms bind-fields, it seems to be empty. If I conj something to it as shown, only the bit conjed in shows. If instead I pass the commented ratom, I get the original content plus the conjed bit.

hjrnunes10:03:36

I know the subs ratom is not empty because the :label before bind-fields always prints what I would expect...

hjrnunes10:03:13

Summing up: :label prints sub correctly, but :select in the form doesn't

hjrnunes11:03:10

Furthermore, if I log the reaction inside new-collection-form template, I can see it initially being nil (there’s an ajax call that populates it), and then filling up (presumably when the ajax call completes). So, now I’m even more baffled: if it logs correctly, why isn’t the for loop seeing it?

nberger12:03:17

@hjrnunes try with [bind-fields [new-collection-form-template (conj @apps {:name "TEST APP"})] form] (you want a vector instead of applying the component fn)

hjrnunes13:03:03

@nberger: thanks. Same result though. Anyway, my understanding from reagent-forms is that bind-fields doesn’t actually expect a component but a data structure that it will walk and wire-up to the form

hjrnunes13:03:06

the 'new-collection-form-template’ function isn’t supposed to be a component but only a template for the form

nberger13:03:09

hmmm I don't know much about reagent-forms, let me check it out...

hjrnunes13:03:11

“The templates are eagerly evaluated, and you should always call the helper functions as in the example above instead of putting them in a vector. These will be replaced by Reagent components when the bind-field is called to compile the template."

hjrnunes13:03:21

“The bind-fields function expects a data structure as its input and walks it to find any elements with a :field attribute."

nberger13:03:35

@hjrnunes: I'm lost there, sorry I can't help

mccraigmccraig15:03:13

@hjrnunes how are you using reagent-forms with re-frame ? if you are using the app-db as the source of data for a view, that's not compatible with reagent-forms binding form fields into an atom - you would have to have separate atoms for the data backing the form fields ?

hjrnunes16:03:19

@mccraigmccraig: yep, I’m using a separate atom as per the reagent-form docs. I pass it’s content to a re-frame event handler via dispatch when its submit time

hjrnunes16:03:13

in snippet you can see it - it’s the “form” ratom. But I need to build that :select list with app-db data, which I’m getting via subscribe. But I can’t seem to be able to get to the subs data inside the form template, even though it still logs fine… 😩

hjrnunes16:03:32

I wonder if it has to do with rendering, because I can see the reaction updating via js/log

hjrnunes16:03:17

I just don’t know enough react/reagent to figure this out

hjrnunes16:03:22

OTH, if the form isn’t re-rendering when the reaction updates, how come I see that js/log result in the console two times? One prints nil, the other actually prints the data I need. So it would seem to me that the component is actually being re-rendered, otherwise that log statement wouldn’t execute twice, right?

fasiha21:03:36

I'm trying to wrap my head around when re-frame will rerender components and when it wont (trying to internalize the lesson of https://github.com/Day8/re-frame#a-more-efficient-signal-graph). I notice that, if one of my subscriptions is very simple: (re-frame/register-sub :name (fn [db] (reaction (:name @db)))), if another part of the database changes, a component subscribed to :name will rerender (though (:name db) didn't change). I can tweak that subscription reaction to this: (re-frame/register-sub :name (fn [db] (let [name (reaction (:name @db))] (reaction @name)))) and indeed this prevents components subscribed to :name from rendering! But I wonder if this is the kind of optimization that the Reagent readme warns is not necessary?

fasiha21:03:29

(because, presumably, even though re-frame might re-render a component subscribed to :name, react won't write it to the DOM since it's not changed)

mikethompson23:03:51

@fasiha: The render function itself is wrapped in a reaction, so if name doesn't change, the render function won't get called

mikethompson23:03:42

@hjrnunes: @fasiha the best way to get a sense of what is happening (the flow), is to use clairvoyant in this manner: https://github.com/Day8/re-frame/wiki/Debugging

mikethompson23:03:21

It is a small bit of work to setup, but then you can just watch the 4 domino pattern happening in the js/console