我们正在开始使用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 []))当发生错误时,捕获它并将其添加到错误中,而不需要交换应用程序状态(应用程序仍处于最后一个稳定状态)。所以我们用链子上的抛球和一种尝试式的仙人掌
(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))))因此,我希望错误会被捕捉到,而@ error就会被捕获。但现实是:
*不知道有一个错误
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:9669 9669batchedUpdates@ react-with-addons.inc.js:16633ReactEventListener.dispatchEvent @ react-with-addons.inc.js:11423*
发布于 2015-12-16 14:18:52
(catch :default e
(swap! errors conj e))))因为抛出的字符串不是对象,而是字符串!
https://stackoverflow.com/questions/34312417
复制相似问题