File tree Expand file tree Collapse file tree 4 files changed +28
-14
lines changed
Expand file tree Collapse file tree 4 files changed +28
-14
lines changed Original file line number Diff line number Diff line change 11import WarpRouter from 'warp-router'
22import { Configuration } from "../../src/Configuration" ;
3+ import { Route } from "../../src/Route" ;
34
45function createDemo ( ) {
5- let routes : Map < string , ( ) => string > = new Map ( )
6- routes . set ( "" , function ( ) {
6+ let routes : Map < string , Route > = new Map ( )
7+ routes . set ( "" , new Route ( function ( ) {
78 return "This is the home page"
8- } )
9- routes . set ( "#page1" , function ( ) {
10- return "This is page 1"
11- } )
12- routes . set ( "#page2" , function ( ) {
13- return "This is page 2"
14- } )
9+ } ) )
10+ routes . set ( "#page1" , new Route ( function ( ) {
11+ return "This uses a custom context"
12+ } ) )
1513
1614 const configuration = new Configuration ( )
1715 configuration . defaultRoute = ""
Original file line number Diff line number Diff line change 1010 "typescript" : " ^2.1.4" ,
1111 "webpack" : " ^2.2.0" ,
1212 "webpack-build-notifier" : " ^0.1.13"
13+ },
14+ "dependencies" : {
15+ "money-input" : " 0.0.1"
1316 }
1417}
Original file line number Diff line number Diff line change 1+ export type RoutingFunction = ( ( context ?: any ) => ( void | string ) )
2+
3+ export class Route {
4+ cacheResult : false
5+
6+ constructor ( public routingFunction : RoutingFunction , public context = null ) {
7+
8+ }
9+ }
Original file line number Diff line number Diff line change 11import { Configuration } from "./Configuration" ;
2+ import { Route } from "./Route" ;
23export default class WarpRouter {
34
45 private hostElement : HTMLElement
56
67 /**
78 *
89 * @param selector where the router will be embedded
9- * @param routes
10+ * @param routes can take a function that will generate a string
1011 */
1112 constructor ( public selector : string ,
12- public routes : Map < string , ( ) => string > ,
13+ public routes : Map < string , Route > ,
1314 public configuration : Configuration = new Configuration ( ) ) {
1415
1516 this . hostElement = < HTMLElement > document . querySelector ( selector )
@@ -21,10 +22,13 @@ export default class WarpRouter {
2122 }
2223
2324
24- applyRouteContentFunction ( route : string ) {
25+ applyRouteContentFunction ( routeString : string ) {
2526 try {
26- const contentFunction = this . routes . get ( route )
27- this . hostElement . innerHTML = contentFunction ( )
27+ const route : Route = this . routes . get ( routeString )
28+ const result = route . routingFunction ( )
29+ if ( typeof result === "string" ) {
30+ this . hostElement . innerHTML = result
31+ }
2832 }
2933 catch ( e ) {
3034 throw new Error ( "Unrecognised route" )
You can’t perform that action at this time.
0 commit comments