This is a repository for a proof of concept for cerebral-module-router
. It is also a second bid for inclusion of this PR: cerebral-legacy/cerebral-module-router#92
Note! This repository is currently using a forked version of cerebral-module-router
https://github.com/awei01/cerebral-module-router/
Update! While this repository uses a forked version of cerebral-module-router
https://github.com/awei01/cerebral-module-router/, you can actually achieve the same effect by pre-pending a factory-generated action, which outputs a signal to the input, before triaging the route. Thus, my fork of cerebral-module-router
is not required. I haven't updated the code to reflect this change, though. If you're interested in seeing the changes, let me know.
Sometimes you want to open a modal window from a variety of routes. See here: cerebral-legacy/cerebral-module-router#96
Sometimes you want to perform a check before changing routes. See here: cerebral-legacy/cerebral-module-router#93. Note that this does not handle a manual change of the URL. I'm sure that this could be handled by incorprating something like this: http://stackoverflow.com/questions/2029343/how-to-stop-window-unloading. I'll probably explore in future versions.
I've enhanced this design pattern somewhat in my own application. My version can now 1) stop execution of a route, 2) enforce a previously stopped execution. If anyone is interested, submit an issue and I'll update this repo to refelct my new developments. Too lazy to include ATM...
Code is kind of messy. I didn't spend much time on organization. But, should be fairly easy to grok. If you have any questions, please create an issue. If you have any suggestions or ideas to make this more cerebral
, please submit an issue.
If you notice any use cases that I have missed, please submit an issue.
- If you want to add a check to ensure that a route can be routed or not, use the service
addRouteCheck
with a callback. The callback will be called withstate.get
- If you want to remove the route check, use the service
removeRouteCheck
with the same callback. - Before each route, check that the route is allowed to be routed by looping through all added route checks.
- If it's okay, then proceed and
storeRoute
so that we can look back at previous route. (This is why I am using my fork ofcerebral-module-router
. I have agetMatchedRoute
method) - If it's not okay, then reject the route and replace the browser history with the previous route. Optionally add some message for the UI. Also, store a flag in
state
noting that we've been rejected. - If the route has been rejected, you can optionally perform some other actions. Clear out the flag in
state
, so we can once again deny the route if necessary.
On lines 28-39 of modules/App/index.js
. I cannot understand WTF is going on. The input gets messed up in the second function of the signal. If anyone can offer any insight, I'd greatly appreciate it.
Anyway, hope this helps someone.