Giter Club home page Giter Club logo

re-frame-routing's Introduction

re-frame-routing

ClojureScript (re-frame) library that manages routing and route state.

re-frame-routing is built on top of bidi and persists route state to re-frame's app state. Registering re-frame-routing to an existing re-frame application is ease.

Step 1: Install

Clojars Project

CircleCI

Step 2: Import Events

(ns app.events.core
  (:require [re-frame-routing.core :as rfr]

            [app.router.core :as router]))

(rfr/register-events {:routes router/routes})

Step 3: Import Subscriptions

(ns app.subscriptions.core
  (:require [re-frame-routing.core :as rfr]))

(rfr/register-subscriptions)

Route Middleware

(Note this API is subject to change in future releases)

Pre Dom Render Loading

Sometimes it's nice to perform work when a route is loading, maybe even prevent the route's view from displaying until that work is done, or even redirect if a user does not have access to view the route. These are good use cases for route-middleware.

To add route-middleware to a route, first create the route-middleware function

(def route-middleware
  (rfr/create-route-middleware {:loading-view loading/render}))

loading-view is a reagent component that will display while your route is loading.

Now let's say you have a sign-in page, but only unauthenticated users should be able to view it. Here is you sign-in route with no middleware

(defmethod containers :sign-in []
  [sign-in/render])

And now we add some middleware

(defmethod containers :sign-in []
  [(route-middleware
    sign-in/render
    [redirect-authenticated])])

Without diving into redirect-authenticated quite yet, let's first look at our route-middleware function. It takes in a view component and a vector of middleware functions (executed from left to right). The result after all functions have executed is the view. The view will receive path and query params as arguments.

Next let's look at redirect-authenticated

(defn redirect-authenticated
  "Redirects authenticated users to the authenticated home page"
  [{:keys [route-query route-params] :as ctx}]
  (let [is-authenticated (re-frame/subscribe [:user/is-authenticated?])]

    (when (true? @is-authenticated)
      (re-frame/dispatch [:router/nav-to "/authenticated-home"]))

    (if (true? @is-authenticated)
      (assoc ctx :is-loading true)
      ctx)))

Middleware functions will be passed a context object and are expected to return a context object. To prevent the execution of the next middleware function in the chain, toggle the is-loading property on the middleware context object to true.

Route Coercion

Given data sent to the servers and to the component originates from information synced in the url (path and query params), it's ideal to have a way to share concerns over configuration (coercion and defaults). The library supports that concern by allowing you to supply an routes-enirched tree where leafs can be supplied a configuration instead of a keyword. e.g

{:query {:site {:coercion int?}
         :role {:coercion int?}
         :learner {:coercion #{"least-ready" "most-ready" "highest-mindset" "lowest-mindset"}
                   :default "least-ready"}
         :current-page {:coercion int?
                :default 0}}}

this will then provide a route-parameters map to the component. e.g

{:route-parameters {:role 1 :learner "least-ready" :site 2 :current-page 0}}

This functionality is then handled by spec-tools. Overall this is a less feature rich version of what's provided by reitit.

Development

Run clj -M:shadow-cljs watch app to and get a watched build. See Shadow cljs docs for more.

License

Copyright © 2018 Matt O'Connell

Distributed under the Eclipse Public License.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.