1

我们开始在产品的下一个开发阶段使用 Clojuescript/Reagent。基本上我们需要一种方法,对原子进行单个操作,我们想出了一个方法,例如: (def app-state (r/atom {}))

(defn select [index]
    (swap! app-state
       #(-> %
            (assoc-in [:storyboard :pages 0 :layers 0 :children 0 :is_selected] true)
            (assoc-in (GET ....)) ;; ajax call
            (assoc-in [:selected :selected_tab] "tab_properties")
            (assoc-in [:selected :page_object_cursor] [0])
         )))

基本上,交换获得了一个功能,其中所有需要的操作都被链接起来。这是辉煌的。但是,我正在寻找处理错误的方法。我想有一个单独的原子来处理错误:

(def errors (r/atom []))

当错误发生时,捕获它并将其添加到错误中,而不需要交换应用程序状态(应用程序保持在最后一个稳定状态)。所以我们使用 throw from the chain 和 try cactch

(defn change-title [title]
  (try
    (swap! app-state
       #(-> %
            (assoc :text title)
            ((fn [state] 
               (throw "there is an error")))
            ))
    (catch js/Object e
      (swap! errors conj e))))

所以我希望错误被捕获,并且@errors 有它。但现实是:

*未捕获有错误

weather_app$core$change_title @ core.cljs?rel=1450266738018:59(匿名函数)@core.cljs?rel=1450266738018:108executeDispatch @ react-with-addons.inc.js:3311SimpleEventPlugin.executeDispatch @ react-with-addons。 inc.js:17428forEachEventDispatch @ react-with-addons.inc.js:3299executeDispatchesInOrder @ react-with-addons.inc.js:3320executeDispatchesAndRelease @ react-with-addons.inc.js:2693forEachAccumulated @ react-with-addons.inc。 js:19430EventPluginHub.processEventQueue@react-with-addons.inc.js:2900runEventQueueInBatch@react-with-addons.inc.js:11217ReactEventEmitterMixin.handleTopLevel@react-with-addons.inc.js:11243handleTopLevelImpl@react-with-addons。 inc.js:11329Mixin.perform @ react-with-addons.inc.js:18402ReactDefaultBatchingStrategy.batchedUpdates @ react-with-addons.inc.js:9669batchedUpdates @ react-with-addons.inc.js:16633ReactEventListener.dispatchEvent@react-with-addons.inc.js:11423*

4

1 回答 1

0
(catch :default e
  (swap! errors conj e))))

因为抛出的字符串不是对象,而是字符串!

于 2015-12-16T14:18:52.210 回答