Skip to content

Instantly share code, notes, and snippets.

@fredantell
Created April 16, 2014 21:16
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 fredantell/10933968 to your computer and use it in GitHub Desktop.
Save fredantell/10933968 to your computer and use it in GitHub Desktop.
kernel time search timeout function
(defn handle-search-change [event owner state]
"Takes the event value, which represents a string the user entered into the search field.
After updating the state, we sleep using a timeout. On wake query the component for the most up to date value. Then wake and check if the component's most up to date state still matches what was passed to the function.
If it is, that means no changes to the desired search string have occurred. Put the search string onto a channel where it can be consumed elsewhere to make an ajax call. If the values don't match, then do nothing."
(let [e-val (.. event -target -value)]
(om/set-state! owner :text e-val)
(go
(<! (timeout 1000))
(let [should-send? (= e-val (om/get-state owner :text))]
(if should-send? (put! (:query state) e-val))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment