This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2015-09-16
Channels
- # admin-announcements (27)
- # beginners (17)
- # boot (216)
- # cider (13)
- # cljs-dev (4)
- # clojure (103)
- # clojure-berlin (2)
- # clojure-dev (18)
- # clojure-italy (14)
- # clojure-japan (1)
- # clojure-nl (4)
- # clojure-norway (1)
- # clojure-russia (8)
- # clojurescript (291)
- # clojurex (12)
- # datomic (31)
- # editors (1)
- # events (16)
- # hoplon (60)
- # jobs (1)
- # ldnclj (85)
- # luminus (15)
- # onyx (2)
- # re-frame (18)
- # reagent (36)
- # remote-jobs (3)
- # yada (3)
@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
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.
Approach 2 is probably the easiest. Method: see the first point in here: https://github.com/Day8/re-frame/wiki/More-advanced-Reagent-techniques
Where you currently use some-entity-view
, add a key using id
^{:key (str id)}[some-entity-view id]
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!
@roberto: subscription handlers are *exactly* where you'd use reaction
. The code provided by @petrus is completely standard in that respect.
Hey, I’m having a bunch of trouble with re-frame/reagent dropping characters in an input field when they’re typed too quickly
I’ve upgraded to reagent 0.5.1 but it hasn’t helped.
The component I’m using is roughly like this:
(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]))))}]))
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
@timgilbert At the moment I'm using dispatch-sync instead of dispatch in my forms for this reason.
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
Then on-change, I dispatch an event to update the model
So it works, but if the model is updated, the field won’t be updated automatically. (Which is fine for my application)
But I might play around with dispatch-sync instead. Have you noticed any problems with it?
@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.