ClojureScript circular dependency

I am struggling with circular addiction in ClojureScript. I test this language for a month, never worked with real (Clojure).

I have a client application that uses secretary as a router. When I define my routes, they perform the functions of a handler, click values โ€‹โ€‹on history-channel, which are then consumed by the main application component that displays certain views. So the values โ€‹โ€‹that I click on my routes contain a link to the function view. This view function is an om component that displays a given location. In these presentation functions, I often need to create links, URLs elsewhere in the application. These URLs are generated from the same handler functions that reference them. This is how my circular addiction is born. What is an elegant way to solve it?

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)}]]))))
+4
2

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)}]]))))

.

+2

, route/some URL-.

Router (history.cljs) :

        [react.nav :as nav]
        [react.loading :as loading]
        [react.alerts :as alerts]
........
(def history (History.))

(events/listen
 history (.-NAVIGATE goog.history.EventType)
 (fn [e]
   (secretary/dispatch! (.-token e))))

(defroute "alerts" {:as params}
  (nav/switch-to "Alerts")
  (alerts/display))

. :

(def history (History.))
   .....
          :on-click (fn [e]
            (.preventDefault e)
            (.setToken history link))}

setToken : (str "alerts/" sort-key "/" filter-str)

0

Source: https://habr.com/ru/post/1541099/


All Articles