-
Notifications
You must be signed in to change notification settings - Fork 597
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
onRoute hook #155
Comments
as discussed on IRC, def sounds like a useful addition |
Cool idea. Do you think it provides value over just wrapping your routes? ie. const auth = (view) => (state, prev, send) => {
return state.authenticated ? view(state, prev, send) : loginView(state, prev, send)
}
app.router((route => [
route('/', homeView),
route('/account', auth(accountView)),
route('/purchases', auth(purchasesView))
]) |
@timwis that's a cool way of doing it, didn't think of that. Another related feature that const app = choo({
onRoute (route, state, prev, send) {
if (route.auth && !state.authenticated) {
return send('location:setLocation', { location: '/login' }) //redirect
}
document.title = 'AppName - ' + route.title // could be cool to incorporate params too...
// default behavior
}
})
app.router((route) => [
route('/login', { auth: false, title: 'Login' }, loginView),
route('/protected', { auth: true, title: 'Protected' }, protectedView)
])
app.start() Not sure what the best way with the wrap approach would be. |
Yeah that would be handy. I imagine that could be done in the view's |
I was just about to create a new issue, but I think my case is similar enough to this one: Basically I want to initialize the model with an asynchronous call (e.g. XHR). I played around with an I can't tell whether there's already a better / more clear way to do this or if that's something that could be addressed with this feature? |
@mantoni Hmmm, the way I view this is:
|
@yoshuawuyts Can you explain where you see this being different in the |
@yoshuawuyts Sorry, ignore me. I just saw that |
Hmmm, though on the other hand... this might make Glad the solution @timwis proposed should at least provide a workable experience right now :) |
For discussion, here's another use case: logging. In my apps I'll often want the browser to tell the server which routes/params are being used. |
@davidguttman hmm, that should already be possible with the |
Came here based on the use case of a header marking a nav element as current, or active, based on where one was in the app. I could read it based on app.state.location, after I clean it up, or it'd be neat if I could just listen to it as a effects, perhaps? (Edit: I meant to write effects instead of subscriptions, currently namespacing doesn't allow this AFAICS) |
Hey @saem, I also spent some time figuring out how to set my active menu item. I kept getting into situations where I changed the state and triggering an infinite loop. I'm using a sidebar and only want to render the content in the area next to it, to do that I was already wrapping my views in a function like @timwis did with the auth example. It was then a matter of passing also the route name to that function and do another check. app.router('/', (route) => [
route('/', subview(views.home, '/')),
route('/about', subview(views.articles, '/about'))
]);
const subview = (view, route) => (state, prev, send) => {
const { activeRoute } = state.sidebar;
if (route !== activeRoute) {
send('sidebar:setActiveRoute', route);
};
return views.main(state, prev, send, view)
}; I have a model for my sidebar which keeps track of the active route, menu items and whether or not it's expanded (mobile). It seems to do the job pretty good but I'm sure there will be a more elegant way of doing this. If anyone has one please shout out. Cheers! |
I think this question is answered. Thanks for opening! |
For certain things like authenticated routes, it would be useful to have a function that runs before each route change so that authentication can be checked and the user can be redirected. Something like:
The text was updated successfully, but these errors were encountered: