Fork me on GitHub
#re-frame
<
2015-09-16
>
mikethompson02:09:27

@petrus: yes, you are capturing the initial value for id. So that subscription is for the original id and does not change when id changes

mikethompson02:09:03

So what you want is a "dynamic subscription". There's two ways of doing that: 1. https://github.com/Day8/re-frame/pull/108 (not officially in re-frame yet, just a PR, use a fork) 2. cheat: actually destroy the component and recreate it each time id changes.

mikethompson02:09:10

Approach 2 is probably the easiest. Method: see the first point in here: https://github.com/Day8/re-frame/wiki/More-advanced-Reagent-techniques

mikethompson02:09:46

Where you currently use some-entity-view, add a key using id

^{:key  (str id)}[some-entity-view id]

mikethompson02:09:16

When id changes, the key changes and the ENTIRE component will be destroyed and recreated, which means the subscription will be recreated for the new id. I said it was a cheat!

mikethompson09:09:36

@roberto: subscription handlers are *exactly* where you'd use reaction. The code provided by @petrus is completely standard in that respect.

timgilbert21:09:45

Hey, I’m having a bunch of trouble with re-frame/reagent dropping characters in an input field when they’re typed too quickly

timgilbert21:09:14

I’ve upgraded to reagent 0.5.1 but it hasn’t helped.

timgilbert21:09:29

The component I’m using is roughly like this:

timgilbert21:09:38

(defn testing []
  (let [value (re-frame/subscribe [:edit-value :temp-strings :example])]
    [:input {:value @value
             :on-change (fn [event]
                          (let [new-value (.. event -target -value)]
                            (when (not= @value new-value)
                              (re-frame/dispatch [:field-changed :temp-strings :example new-value]))))}]))

timgilbert22:09:43

Any pointers? I’m not having the cursor-jumping problem I used to have, but my forms are not super usable for fast typists right now

Tom H.23:09:27

@timgilbert At the moment I'm using dispatch-sync instead of dispatch in my forms for this reason.

timgilbert23:09:56

Thanks @tomisme. I’ve found a workaround by having the input text bound to an internal atom created on mount, with its value initialized from the model

timgilbert23:09:16

Then on-change, I dispatch an event to update the model

timgilbert23:09:51

So it works, but if the model is updated, the field won’t be updated automatically. (Which is fine for my application)

timgilbert23:09:13

But I might play around with dispatch-sync instead. Have you noticed any problems with it?

Tom H.23:09:59

@timgilbert no, it's been working fine so far. Another alternative is to dispatch :on-blur instead of :on-change. This means that your model will change when the input box is focused or unfocused by the user.

Tom H.23:09:15

My app doesn't have transition animations at the moment, however. Which could possibly be affected by dispatch-sync in certain cases (typing while something is moving).