Skip to content

a POC for cerebral-module-router handling of modals and route prevention

Notifications You must be signed in to change notification settings

awei01/cerebral-module-router-POC

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cerebral-module-router POC

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.

Motivation

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.

More Developed Version

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...

Suggestions?

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.

General Concepts

  1. 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 with state.get
  2. If you want to remove the route check, use the service removeRouteCheck with the same callback.
  3. Before each route, check that the route is allowed to be routed by looping through all added route checks.
  4. 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 of cerebral-module-router. I have a getMatchedRoute method)
  5. 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.
  6. 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.

Bonus Question

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.

About

a POC for cerebral-module-router handling of modals and route prevention

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published