>From reading the doc at
https://github.com/Day8/re-frame/blob/master/docs/Subscribing-To-External-Data.md
it looks like the two "normal" patterns for getting data from external 
sources are 

(1) a subscription handler to trigger the request and return a path in the 
app state, coupled with a callback that dispatches an event 
to populate that path when the external service responds

(2) an event to trigger the request and another event that dispatches when 
its callback runs, to save the response somewhere in app state

and then there's some sternly worded advice against putting this stuff into 
view functions, with which I concur absolutely

My question is: why put the results into app-db at all?  If you start with 
the attitude that the external service is functional - i.e. it gives the 
same answer every time given the same parameter, then what if you had a 
subscription handler that returned a reaction with the results of the 
external service call?  I'm thinking something like this:

(defn get-search-results-from-server [term handler]
  (ajax-request
    {:method          :get
     :uri             "/search"
     :timeout 5000
     :params          {:q term :limit 25}
     :format          (ajax/url-request-format)
     :response-format (ajax/json-response-format {:keywords? true})
     :handler handler}))


(rf/reg-sub-raw
  :search-result
  (fn [db _]
    (let [out (reagent/atom [])]
      (ratom/run!
       (let [term (rf/subscribe [:search-term])]
         (when-not (str/blank? term)
           (reset! out [])
           (get-search-results-from-server
             term
             (fn [[ok? new-value]] (and ok? (reset! out new-value)))))))
      (reaction out))))


- i.e. it's just another subscription handler that sits somewhere in the 
middle of the signal graph and computes things, albeit that it computes 
them very slowly.


It seems to work in my app (see 
https://github.com/telent/epsilon/blob/master/src/epsilon/client.cljs#L217) 
but my understanding of ratoms and reactions right now is very superficial, 
so it's not beyond the bounds of possibility that it only works by 
accident.  

Thanks, by the way, for a very lovely bit of software and some exceedingly 
good documentation around it

-dan

-- 
You received this message because you are subscribed to the Google Groups 
"Reagent-Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to reagent-project+unsubscr...@googlegroups.com.
To post to this group, send email to reagent-project@googlegroups.com.
Visit this group at https://groups.google.com/group/reagent-project.
For more options, visit https://groups.google.com/d/optout.

Reply via email to