Search code examples
clojureurl-routingsecretary

Clojure Secretary - Dispatch on URL Change


Can Secretary dispatch routes based on a submitted browser url, the way many javascript routing frameworks work? I can't seem to find any examples of wiring this up.

For example, when you enter http://myapp.com/#/my/route in the browser's url, I'd like that to dispatch as though I had programatically entered (secretary/dispatch! "/my/route") in the repl.


Solution

  • It can, but not by itself. The most common way of doing this is with Google Closure, as in the README.md example:

    (let [h (History.)]
      (goog.events/listen h EventType/NAVIGATE #(secretary/dispatch! (.-token %)))
      (doto h (.setEnabled true)))
    

    When you call setEnabled in the example above, the NAVIGATE event will automatically fire for the current location which will cause dispatch! to be invoked.