|
| 1 | +# Route Helper Utilities |
| 2 | + |
| 3 | +This document explains how to use the route helper utilities to simplify adding and managing routes in the SPA application. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The route helper utilities provide a more concise and maintainable way to define routes for the SPA router. They handle common patterns like authentication checks, subscription requirements, and page initialization. |
| 8 | + |
| 9 | +## Key Features |
| 10 | + |
| 11 | +- **Simplified Route Definition**: Define routes with minimal code |
| 12 | +- **Authentication Handling**: Built-in support for protected routes |
| 13 | +- **Subscription Checks**: Easy way to require active subscriptions for premium routes |
| 14 | +- **Modular Route Organization**: Support for organizing routes in separate files |
| 15 | +- **Consistent Route Behavior**: Standardized handling of route transitions and guards |
| 16 | + |
| 17 | +## Usage |
| 18 | + |
| 19 | +### Basic Route Definition |
| 20 | + |
| 21 | +The simplest way to define a route is to provide just the path to the view HTML file: |
| 22 | + |
| 23 | +```javascript |
| 24 | +import { createRoutes } from './route-helpers.js'; |
| 25 | + |
| 26 | +const routes = createRoutes({ |
| 27 | + '/about': '/views/about.html' |
| 28 | +}); |
| 29 | +``` |
| 30 | + |
| 31 | +### Route with Initialization Function |
| 32 | + |
| 33 | +To run code after a route is rendered, use the `afterRender` option: |
| 34 | + |
| 35 | +```javascript |
| 36 | +const routes = createRoutes({ |
| 37 | + '/profile': { |
| 38 | + viewPath: '/views/profile.html', |
| 39 | + afterRender: () => initProfilePage() |
| 40 | + } |
| 41 | +}); |
| 42 | +``` |
| 43 | + |
| 44 | +### Protected Routes |
| 45 | + |
| 46 | +To require authentication for a route, use the `requireAuth` option: |
| 47 | + |
| 48 | +```javascript |
| 49 | +const routes = createRoutes({ |
| 50 | + '/dashboard': { |
| 51 | + viewPath: '/views/dashboard.html', |
| 52 | + requireAuth: true |
| 53 | + } |
| 54 | +}); |
| 55 | +``` |
| 56 | + |
| 57 | +### Premium Routes |
| 58 | + |
| 59 | +To require an active subscription, use the `requireSubscription` option: |
| 60 | + |
| 61 | +```javascript |
| 62 | +const routes = createRoutes({ |
| 63 | + '/premium-feature': { |
| 64 | + viewPath: '/views/premium-feature.html', |
| 65 | + requireSubscription: true |
| 66 | + } |
| 67 | +}); |
| 68 | +``` |
| 69 | + |
| 70 | +### Custom Route Guards |
| 71 | + |
| 72 | +For more complex access control, use the `beforeEnter` option: |
| 73 | + |
| 74 | +```javascript |
| 75 | +const routes = createRoutes({ |
| 76 | + '/admin': { |
| 77 | + viewPath: '/views/admin.html', |
| 78 | + beforeEnter: (to, from, next) => { |
| 79 | + const userRole = localStorage.getItem('user_role'); |
| 80 | + if (userRole === 'admin') { |
| 81 | + next(); |
| 82 | + } else { |
| 83 | + next('/access-denied'); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | +}); |
| 88 | +``` |
| 89 | + |
| 90 | +## Organizing Routes |
| 91 | + |
| 92 | +For larger applications, you can organize routes in separate files: |
| 93 | + |
| 94 | +### Feature-specific Routes |
| 95 | + |
| 96 | +```javascript |
| 97 | +// feature-routes.js |
| 98 | +import { createRoutes } from './route-helpers.js'; |
| 99 | +import { initFeaturePage } from './page-initializers.js'; |
| 100 | + |
| 101 | +export const featureRoutes = createRoutes({ |
| 102 | + '/feature': { |
| 103 | + viewPath: '/views/feature.html', |
| 104 | + afterRender: initFeaturePage |
| 105 | + }, |
| 106 | + '/feature/settings': { |
| 107 | + viewPath: '/views/feature-settings.html', |
| 108 | + requireAuth: true |
| 109 | + } |
| 110 | +}); |
| 111 | +``` |
| 112 | + |
| 113 | +### Merging Routes |
| 114 | + |
| 115 | +```javascript |
| 116 | +// router.js |
| 117 | +import { createRoutes } from './route-helpers.js'; |
| 118 | +import { featureRoutes } from './feature-routes.js'; |
| 119 | +import { adminRoutes } from './admin-routes.js'; |
| 120 | + |
| 121 | +export function defineRoutes(router) { |
| 122 | + // Define core routes |
| 123 | + const coreRoutes = createRoutes({ |
| 124 | + '/': '/views/home.html', |
| 125 | + '/login': { |
| 126 | + viewPath: '/views/login.html', |
| 127 | + afterRender: initLoginPage |
| 128 | + } |
| 129 | + }); |
| 130 | + |
| 131 | + // Merge all routes |
| 132 | + const routes = { |
| 133 | + ...coreRoutes, |
| 134 | + ...featureRoutes, |
| 135 | + ...adminRoutes |
| 136 | + }; |
| 137 | + |
| 138 | + // Register routes with the router |
| 139 | + router.registerRoutes(routes); |
| 140 | + router.init(); |
| 141 | + |
| 142 | + return router; |
| 143 | +} |
| 144 | +``` |
| 145 | + |
| 146 | +## Adding a New Route |
| 147 | + |
| 148 | +To add a new route to the application: |
| 149 | + |
| 150 | +1. Create the HTML view file in the `/views` directory |
| 151 | +2. If needed, create an initialization function in `page-initializers.js` |
| 152 | +3. Add the route to the routes object in `router.js` using the `createRoutes` helper |
| 153 | + |
| 154 | +Example: |
| 155 | + |
| 156 | +```javascript |
| 157 | +// In router.js |
| 158 | +const routes = createRoutes({ |
| 159 | + // ... existing routes |
| 160 | + |
| 161 | + // Add your new route |
| 162 | + '/new-feature': { |
| 163 | + viewPath: '/views/new-feature.html', |
| 164 | + afterRender: initNewFeaturePage, |
| 165 | + requireAuth: true // If the route requires authentication |
| 166 | + } |
| 167 | +}); |
| 168 | +``` |
| 169 | + |
| 170 | +For more detailed examples, see the `examples/add-new-route-example.js` file. |
| 171 | + |
| 172 | +## API Reference |
| 173 | + |
| 174 | +### `createRoute(viewPath, options)` |
| 175 | + |
| 176 | +Creates a single route configuration object. |
| 177 | + |
| 178 | +Parameters: |
| 179 | +- `viewPath`: Path to the view HTML file |
| 180 | +- `options`: Route options |
| 181 | + - `afterRender`: Function to run after rendering |
| 182 | + - `beforeEnter`: Function to run before entering route |
| 183 | + - `requireAuth`: Whether the route requires authentication |
| 184 | + - `requireSubscription`: Whether the route requires an active subscription |
| 185 | + |
| 186 | +### `createRoutes(routeDefinitions)` |
| 187 | + |
| 188 | +Creates multiple route configuration objects. |
| 189 | + |
| 190 | +Parameters: |
| 191 | +- `routeDefinitions`: Object mapping paths to route definitions |
| 192 | + - Keys are route paths |
| 193 | + - Values can be: |
| 194 | + - String: Path to view HTML file |
| 195 | + - Object: Route configuration with `viewPath` and options |
0 commit comments