如何在重新框架中从事件中分派事件

时间:2017-10-09 16:13:59

标签: clojure clojurescript reagent re-frame

我按照这个例子: https://github.com/Day8/re-frame/blob/master/docs/FAQs/PollADatabaseEvery60.md

这是我的间隔处理程序

(defonce interval-handler
  (fn [{:keys [action id frequency event]}]
    (let [live-intervals (atom {})]
      (condp = action
        :start (swap! live-intervals assoc id (js/setInterval #(re-frame/dispatch event) frequency))
        :end (do (js/clearInterval (get live-intervals id))
                 (swap! live-intervals dissoc id))))))

(re-frame/reg-fx
  :interval
  interval-handler)

我试图从另一个事件发送此间隔事件:

(re-frame/reg-event-db
  :start-playing
  (fn [db _]
    (re-frame/dispatch [:interval {:action :start
                       :id :some-awesome-id
                       :frequency 1000
                       :event [:tick]}])
    (assoc db :is-playing? true
           :fake (random-active-color db)
           :real (random-active-color db))))

但它说re-frame: no :event handler registered for: :interval

这不可能吗?

1 个答案:

答案 0 :(得分:4)

(re-frame/reg-event-fx :start-playing (fn [{:keys [db]} _] {:interval {:action :start :id :some-awesome-id :event [:tick]}] :db (assoc db :is-playing? true :fake (random-active-color db) :real (random-active-color db))})) 是一种效果,而不是一种事件。要调用效果,您需要将其作为事件处理程序返回的效果图中的键包含在内 - 而不是使用效果键发出另一个事件:

:db

上面的事件处理程序将返回一个描述两种效果的地图:

  • :db - 将app db更新为新值(以:interval值提供)

  • interval-handler - 重新框架会在效果图中使用:interval条目的值调用效果处理程序(Push Failed: SSH could not write data: Unable to send channel data