Skip to content

Instantly share code, notes, and snippets.

@viksit
Created October 8, 2014 08:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save viksit/353d73081a0152aba0a8 to your computer and use it in GitHub Desktop.
Save viksit/353d73081a0152aba0a8 to your computer and use it in GitHub Desktop.
(def r {:success "true"
:msg "success"
:results [
{:tags "a,b,c"
:content "sfsd"
:title "my title"
}]})
(def r1 {:success "true"
:msg "success"
:results [
{:tags "a,b,c,d,e,f,g"
:content "allice"
:title "my other title"
}]})
(def my-app-state
(atom {:autocomplete []
:cur-search-results r}))
(defn set-active! [app state]
(do
(prn "app: " app " state: " state)
(put! (:search-results-chan state) app)))
(defn autocomplete-view [app owner]
(reify
om/IRenderState
(render-state [_ state]
(dom/div nil
(dom/div nil "Autocomplete")
(dom/button
#js {:onClick
#(set-active! r1 state)
;; Set the channel with r1
;; (put! (:cur-search-results @app) r1)
;; Set the state to r1
;; (prn "clicked!!" state @app)
;; #(om/set-state! owner :cur-search-results r1)
}
"button")))))
(defn process-search-results [results owner]
(do
(println ":cur-search-results for owner " owner "set to : " results)
(om/set-state! owner :cur-search-results results)))
(defn search-results-view [app owner]
(reify
om/IInitState
(init-state [_]
{:search-results-chan (chan)
:cur-search-results (.-value (get-in app [:cur-search-results]))})
om/IWillMount
(will-mount [_]
(let [search-results-chan (om/get-state owner :search-results-chan)]
(.log js/console "Search results channel: " search-results-chan)
(println "Setting up a go loop for this channel")
;; Set up a go channel
(go (while true
;; Update the owner state by putting cur-search-results to what comes
;; out of the channel
(process-search-results (<! search-results-chan) owner)))))
om/IRenderState
(render-state [_ state]
;; When the state updates, refresh this
(let [cur-search-results (:cur-search-results state)] ;; search-results :: current-video
(dom/div nil
(dom/div nil "Search results")
(dom/div nil (str cur-search-results)))))))
(om/root autocomplete-view my-app-state
{:target (js/document.getElementById "autocomplete")})
(om/root search-results-view my-app-state
{:target (js/document.getElementById "timeline")})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment