Frontmatter
| id | 4654 |
| title | Creating a Router for neo |
| state | Closed |
| labels | enhancement |
| assignees | [] |
| createdAt | Aug 6, 2023, 2:09 PM |
| updatedAt | Oct 30, 2023, 10:46 AM |
| githubUrl | https://github.com/neomjs/neo/issues/4654 |
| author | tobiu |
| commentsCount | 1 |
| parentIssue | null |
| subIssues | [] |
| subIssuesCompleted | 0 |
| subIssuesTotal | 0 |
| blockedBy | [] |
| blocking | [] |
| closedAt | Oct 30, 2023, 10:46 AM |
Creating a Router for neo

tobiu
Aug 16, 2023, 8:46 PM
The router should be capable of handling private routes (only available when being logged in) otherwise redirect to login.
We could also add role based routes, in case we do want to make it more generic.
ThorstenRaab cross-referenced by PR #5023 on Oct 17, 2023, 8:29 AM
tobiu closed this issue on Oct 30, 2023, 10:46 AM
Let me explain the current logic first.
main.DomEventswill forward hash-change events to the app worker: https://github.com/neomjs/neo/blob/dev/src/main/DomEvents.mjs#L497worker.Appwill push the new values intoutil.HashHistory: https://github.com/neomjs/neo/blob/dev/src/worker/App.mjs#L277This Singleton will store the last x hash values inside a stack: https://github.com/neomjs/neo/blob/dev/src/util/HashHistory.mjs
Now every controller (including view controllers) will subscribe to the HashHistory change event: https://github.com/neomjs/neo/blob/dev/src/controller/Base.mjs
As a result, developers need to override
onHashChange()inside their controllers and act accordingly to modify their views as needed. This can be cumbersome.It would be nice, if
controller.Basewould provide aroutes_config, where you can directly specify handlers. This should include variables (could be wrapped inside {}), so we need a bit of regex parsing.Example how it could be:
routes: { '/home' : 'handleHomeRoute', '/users/{userId}' : 'handleUserRoute', '/users/{userId}/posts/{postId}': 'handlePostRoute', 'default' : 'handleOtherRoutes' }The route callback handlers should receive params for the new & old route, as well as used ids inside the route definitions.
Since
controller.Basedoes nothing else except for working with hash related changes, I would drop the logic in there. An alternative would be to extend the class =>controller.Router. In this casecontroller.Component(view controller) would need to extend the router.