Advertisement
Guest User

Untitled

a guest
Oct 4th, 2015
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. ; A modified group-by for jsons.
  2. ; Eg. [{:id "id1" :title "uno"} {:id "id2" :title "due"}]
  3. ; to, for example, this: {:id1 {:id "id1 :title "uno"} :id2 {:id "id2" :title "due"}}
  4.  
  5. (defn group-json
  6. [f coll]
  7. (persistent!
  8. (reduce
  9. (fn [ret x]
  10. (let [k (keyword (f x))]
  11. (assoc! ret k x)))
  12. (transient {}) coll)))
  13.  
  14.  
  15.  
  16. ; Get the data with cljs-ajax and update the ref-cursor.
  17. (defn get-results
  18. [cursor limit skip]
  19. (GET MY_URL {:params {:limit limit :skip skip}
  20. :keywords? true
  21. :response-format :json
  22. :handler (fn [results]
  23. (when-not (empty? results)
  24. (om/transact! (cursor)
  25. (fn [curs]
  26. (merge curs (group-json :id results))))))))
  27.  
  28.  
  29.  
  30.  
  31. ; An app state cursor and a ref cursor used by our component.
  32. (defonce app-state
  33. (atom {:stuff {...} :other-stuff {...} :results {}}))
  34.  
  35. (defn results []
  36. (om/ref-cursor (:results (om/root-cursor app-state))))
  37.  
  38.  
  39.  
  40. ; The Om component that renders the elements (using sablono).
  41. (defn first-view [_ owner]
  42. (reify
  43. om/IRender
  44. (render [_]
  45. (let [xs (om/observe owner (results))]
  46. (html
  47. [:div {}
  48. [:ul {}
  49. (for [[k v] xs]
  50. [:li {} (:title v)])]])))))
  51.  
  52.  
  53.  
  54. ; Another component that triggers the query.
  55. (defn second-view [_ owner]
  56. (reify
  57. om/IRender
  58. (render [_]
  59. (html
  60. [:div {}
  61. [:input {:ref "limit" :id "limit" :placeholder "Limit"}]
  62. [:input {:ref "skip" :id "skip :placeholder "Skip}]
  63. [:button {:onClick (fn [_]
  64. (let [limit (om/get-node owner "limit")
  65. skip (om/get-node owner "skip")]
  66. (get-results results (.-value limit) (.-value skip))))
  67. "Fetch results"]
  68. (om/build first-view)]))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement