venantius / accountant

ClojureScript navigation for single-page applications, made simple.
Eclipse Public License 1.0
250 stars 39 forks source link

Manually changing url. #5

Closed chyzwar closed 9 years ago

chyzwar commented 9 years ago

Hi

It should be possible to use secretary and accountant for routing when user type url manually?

venantius commented 9 years ago

I don't understand what you're asking. Could you clarify?

chyzwar commented 9 years ago

When user type in url in browser address bar like: //hostname/login then accountant should try to match this to existing routes.

venantius commented 9 years ago

It does, if you have your secretary routes defined. That's like half the point of the library :stuck_out_tongue:

If you're not seeing that behavior, show me what you're doing with secretary and accountant, because it's probably configured wrong.

chyzwar commented 9 years ago
(ns ^:figwheel-always whisper.routes
  (:require [om.core :as om :include-macros true]
            [om.dom :as dom :include-macros true]
            [secretary.core :as sec :refer-macros [defroute]]
            [accountant.core :as accountant]))

(accountant/configure-navigation!)

(defn widget [data owner]
  (reify
    om/IRender
    (render [this]
            (dom/h1 nil (:text data)))))

(defroute main-page "/" []
  (om/root widget {:text "Main Page"}
           {:target (. js/document (getElementById "app"))})

  (js/console.log (str "Main Page")))

(defroute login-page "/login" []
  (om/root widget {:text "Login Page"}
           {:target (. js/document (getElementById "app"))})
  (js/console.log (str "Login Page")))

(defroute wildcard "*" []
  (accountant/navigate! "/"))

(accountant/navigate! "/login")

Page start on "/login" , but when I change "/" I get empty page. Alternativly, When I start from / and change to /login I got page not found.

I use https://github.com/bhauman/figwheel-template

I just started with cljs, maybe I messed up :)

venantius commented 9 years ago

I do think having the navigate! call there at the end is probably sub-optimal. What I have on load time is (secretary/dispatch! (.-pathname (.-location js/window))) but that's your call.

The only other thing I can think is that your wildcard route is firing somehow. What actually happens when you try to load /login? Does anything show up in the console?

chyzwar commented 9 years ago

When I start figwhell, page is loaded to /login route. But whenever I click on page area I get the error in screenshot. Strenge enough, error is only happen when I click on page.

error

When I remove /login from url leaving only http://localhost:3449 page is loaded but not root or wildcard route is triggered (no console logs) To see console.logs you need to add: (enable-console-print!)

Maybe it is figwhell related issue.

venantius commented 9 years ago

I now understand your issue. (accountant/navigate!) is intended for use as a reactor; it's not intended to be used in the initial page load. A better example of its use can be found here:

(defn go-to-repo-dashboard!
  [username repo]
  (accountant/navigate!
   (string/format "/gh/%s/%s" username repo)))

Instead, you should use the initial dispatch! call that I pasted above for your initial page load:

(accountant/configure-navigation!)

(defn widget [data owner]
  (reify
    om/IRender
    (render [this]
            (dom/h1 nil (:text data)))))

(defroute main-page "/" []
  (om/root widget {:text "Main Page"}
           {:target (. js/document (getElementById "app"))})

  (js/console.log (str "Main Page")))

(defroute login-page "/login" []
  (om/root widget {:text "Login Page"}
           {:target (. js/document (getElementById "app"))})
  (js/console.log (str "Login Page")))

(defroute wildcard "*" []
  (accountant/navigate! "/"))

(secretary/dispatch! (.-pathname (.-location js/window)))

I've tested this and it works great once you change it.

There's a good question here about whether accountant/navigate! should do the thing you expect it to here, and to be totally honest I don't understand why it doesn't. I'll have to look into it further.

venantius commented 9 years ago

Actually, I take that back. accountant/navigate! does work fine for me as well.

venantius commented 9 years ago

Okay, yeah, sorry, I'm completely unable to reproduce your issue. The code you've provided works fine on my machine.

chyzwar commented 9 years ago

thanks, for your time. This is probably related to figwheel ,like it loose state of history of something like that.

On Oct 22 2015, at 1:34 am, Ursa americanus kermodei <notifications@github.com> wrote:

Okay, yeah, sorry, I'm completely unable to reproduce your issue. The code you've provided works fine on my machine.


Reply to this email directly or view it on GitHub.