A Clojurescript library for quick and easy HTML5 pushState.
You can construct a new instance by calling the pushy
function.
pushy
takes in two arguments:
dispatch
fn: gets called when there is a matchmatch
fn: checks if the path matches any routes defined.
Optionally, you can specify an :identity-fn
which parses and returns the route based on the result of the match
fn.
You can start the event listeners with the start!
method.
This adds an event listener to all click
events, which dispatches on all matched routes.
Bypasses on Alt, Shift, Meta, Ctrl keys and middle clicks.
The stop!
method will tear down all event listeners of the pushy instance.
pushy should work with any routing library:
(ns foo.core
(:require [secretary.core :as secretary :include-macros true :refer-macros [defroute]]
[pushy.core :as pushy]))
(secretary/set-config! :prefix "/")
(defroute index "/foo" []
(.log js/console "Hi"))
(def history (pushy/pushy secretary/dispatch!
(fn [x] (when (secretary/locate-route x) x))))
;; Start event listeners
(pushy/start! history)
(ns foo.core
(:require [bidi.bidi :as bidi]
[pushy.core :as pushy]))
(def state (atom {}))
(def app-routes
["/" {"foo" :foo}])
(defn set-page! [match]
(swap! state assoc :page match))
(def history
(pushy/pushy set-page! (partial bidi/match-route app-routes)))
(pushy/start! history)
(ns foo.core
(:require [domkm.silk :as silk]
[pushy.core :as pushy]))
(def state (atom {}))
(def app-routes
(silk/routes [[:foo [["/foo"]]]]))
(defn set-page! [match]
(swap! state assoc :page match))
(def history
(pushy/pushy set-page! (partial silk/arrive app-routes)))
(pushy/start! history)
By default pushy will dispatch on all relative URLs and absolute URLs that match the window's origin. This means that all external links will be bypassed.
It is possible to specify which URLs are processable to pushy by specifying a custom predicate function :processable-url?
in the constructor. This function is passed a single argument which is an instance of goog.URI
.
You can set the history state manually by calling the set-token!
method. This will call the dispatch
fn on a successfully matched path.
Example:
(set-token! history "/foo")
(get-token history)
;; => /foo
Likewise, you can call replace-token!
which will also call the dispatch
fn and replace the current history state without affecting the rest of the history stack.
Copyright © 2014
Distributed under the Eclipse Public License either version 1.0