passing AJAX data into template?

26 views
Skip to first unread message

Paul Lam

unread,
May 27, 2014, 10:56:43 PM5/27/14
to enf...@googlegroups.com
How do I pass AJAX data (via cljs-http) into a deftemplate? Does that even make sense?

I'm probably doing this very wrong. I have a (deftemplate foo [data] ...). Then I'm just passing in the data as such:  (foo (get-some-data)), where get-some-data is my cljs-http.client/get call. The problem is that (get-some-data) keeps returning an object instead of the actual data.

I suppose I might need to manipulate an atom inside (get-some-data) instead of returning directly? If that's the case, how do I bind the atom to the `foo` template please?

Thanks a lot!

ckirkendall

unread,
May 28, 2014, 6:23:34 AM5/28/14
to enf...@googlegroups.com
If I recall cljs-http returns a core.async channel from its get operation.  It does this because the AJAX call is async by nature and doesn't return the data immediately when you make a call.  To get around this we use a 'go' block to read from the channel and push the data to the deftemplate.


(ns example.core
  (:require-macros [cljs.core.async.macros :refer [go]])
  (:require [cljs-http.client :as http]
            [cljs.core.async :refer [<!]]))

(defn get-some-data []
    (http/get ...)

(defn update-view [ch]
  (go (let [resp (<! ch))]
           (foo resp))))

(update-view (get-some-data))

Hope that helps.

Creighton

Paul Lam

unread,
May 28, 2014, 6:25:16 PM5/28/14
to enf...@googlegroups.com
yes, that worked. thank you!
Reply all
Reply to author
Forward
0 new messages