PathRouteBuilder
type PathRouteBuilder<
ActionResult,
Defs extends RoutesDefinition<ActionResult>,
AnyFlag extends WildcardFlagType,
ExactFlag extends WildcardFlagType,
Match
>
The Path route builder is a route builder that defines one level of pathname segments.
Initiaization
Rocon.Path(): PathRouteBuilder<...>
Creates a new instance of Path route builder. It has no routes defined at first.
builder.route(key, callback?)
route<Key extends string>(
key: string,
callback?: (route: PathSingleRouteInterface<...>) => void
): PathRouteBuilder<...>
Returns a new Path route builder with a new route for key
added. A PathSingleRouteInterface object is passed to the callback
function (if any). By providing a callback
function, one can add an action or attach another route builder to the newly added route.
Example
// create a Path route builder with /foo and /bar
const foobarRoutes = Rocon.Path()
.route("foo", (r) => r.action(()=> <p>I am foo</p>))
.route("bar", (r) => r.action(()=> <p>I am bar</p>));
builder.routes(defs)
routes<D extends RoutesDefinition<ActionResult>>(
defs: D
): PathRouteBuilder<...>
Adds multiple routes at once. Actions can be specified for each route. Each key of given object becomes a route key.
Example
// create a Path route builder with /foo and /bar
const foobarRoutes = Rocon.Path()
.routes({
foo: {
action: ()=> <p>I am foo</p>
},
bar: {
action: ()=> <p>I am bar</p>
},
});
builder.exact(routeDefinition)
exact<RD extends RouteDefinition<...>>(
routeDefinition: RD
): PathRouteBuilder<...>
Returns a new Path route builder with an exact route (route for /
) added.
Example
// create a Path route builder with / and /foo
const fooRoutes = Rocon.Path()
.exact({
action: () => <p>I am root</p>
})
.route("foo", (r) => r.action(()=> <p>I am foo</p>))
builder.any(routeDefinition)
any<Key extends string, RD extends RouteDefinition<...>>(
key: Key,
routeDefinition: RD
): PathRouteBuilder<...>
Returns a new Path route builder with an any route (route that catches all paths expect explicitly defined ones) added.key
is a match object key with type string
whose content is the path segment catched by this route.
Example
// Create a Path route builder that catches any path.
// By accessing /foobar you see "You visited /foobar"
const catchAllRoutes = Rocon.Path()
.any("pathId", {
action: ({ pathId }) => <p>You visited /{pathId} </p>
})
builder._
The collection of all named route records defined in this Path route builder.
Example
const foobarRoutes = Rocon.Path()
.routes({
foo: {
action: ()=> <p>I am foo</p>
},
bar: {
action: ()=> <p>I am bar</p>
},
});
console.log(foobarRoutes._.foo);
foobarRoutes._.bar.attach(...);
builder.exactRoute
Route record for an exact route defined in this route. undefined
if no exact route is defined.
builder.anyRoute
Route record for an any route defined in this route.undefined
if no any route is defined.