                       :error :empty-search))
           (om/transact! data 
             #(assoc % :current-state :loaded-final)))
-
         :else
         (om/transact! data
           #(assoc % :current-state :loaded

                 (= (:current-search @data) 
                    (:search-term response)))
      (cond
        (:error response)
        (om/transact! data
          #(assoc % :current-state :error
                    :error (:error response)))

        (empty? (:items response))
        (if (empty? (:current-items @data))
          (om/transact! data

        ch  (chan)]
    (.send req nil #(put! ch %))
    (go (let [[v c] (alts! [t ch])]
          (case
            ; timeout
            (= c t)
            (>! ajax-chan {:error :timeout})
            ; Response !OK
            (not (= (.. v -meta -status) 200))
            (>! ajax-chan {:error :ajax})
            ; response OK
            (let [items (-> (.. v -response)
                            (js->clj :keywordize-keys true)
                            (#(filter #(= (:type %) "photo"))))]
              (>! ajax-chan {:error false
                             :search-term search-term
                             :items items})))))))

(defn new-search [data ajax-chan query]
  (om/transact! data 
    #(assoc % :state :loading
              :before 0
              :current-search query
              :current-items [])))

;; Response Handler
;; -----------------------------------------------------------------------------
