我正在努力克服ClojureScript中的循环依赖.我正在尝试这种语言一个月,没有用过真实的东西(Clojure).
我有一个客户端应用程序,使用secretary作为路由器.当我定义我的路由时,它们是处理函数,将值推送到历史通道,然后由显示特定视图的主应用程序组件使用.因此,我从路由推送的值包含对视图函数的引用.此视图函数是呈现给定位置的om组件.在这些视图功能中,我经常需要生成链接,URL到应用程序中的其他位置.这些URL是从引用它们的相同处理函数生成的.这就是我的循环依赖诞生的方式.解决它的优雅方法是什么?
router -> views -> router
– route.cljs
(ns myapp.route
(:require [secretary.core :as secretary :include-macros true :refer [defroute]]
[myapp.views.welcome :as welcome]
[myapp.views.some :as some]))
(defroute home "/" {}
(put! history-chan {:token "/"
:view welcome/view}))
(defroute some "/some" {}
(put! history-chan {:token "/some"
:view some/view}))
– welcome.cljs
(ns myapp.views.welcome
(:require [om.core :as om :include-macros true]
[sablono.core :as html :refer-macros [html]]
[myapp.route :as route]))
(defn view [state owner]
(reify
om/IRender
(render [_]
(html [:div [:a {:href (route/some)}]]))))
最佳答案 Clojure中的循环依赖关系没有简单或优雅的解决方案.您很可能需要重新构建代码.你将不得不乱用它找到你喜欢的东西.就在我的头顶,我可能会做这样的事情:
– route.cljs
(ns myapp.route
(:require [secretary.core :as secretary :include-macros true :refer [defroute]]
[myapp.views.welcome :as welcome]
[myapp.views.some :as some]))
(defroute home "/" {}
(welcome/route))
(defroute some "/some" {}
(put! history-chan {:token "/some"
:view some/view}))
– welcome.cljs
(ns myapp.views.welcome
(:require [om.core :as om :include-macros true]
[sablono.core :as html :refer-macros [html]]))
(declare view)
(defn route []
(put! history-chan {:token "/"
:view view}))
(defn view [state owner]
(reify
om/IRender
(render [_]
(html [:div [:a {:href (route)}]]))))
这只是一种可能的解决方案.