Skip to content

Commit 05a8554

Browse files
Introduce route class
1 parent 9df6742 commit 05a8554

File tree

4 files changed

+28
-14
lines changed

4 files changed

+28
-14
lines changed

demo/typescriptImport/index.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
import WarpRouter from 'warp-router'
22
import {Configuration} from "../../src/Configuration";
3+
import {Route} from "../../src/Route";
34

45
function 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 = ""

demo/typescriptImport/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,8 @@
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
}

src/Route.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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+
}

src/WarpRouter.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import {Configuration} from "./Configuration";
2+
import {Route} from "./Route";
23
export 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")

0 commit comments

Comments
 (0)