ko.routing: local routers

As a defaul the main router process all links defined in the page whose urls start with "#", and  all other urls cause a physical page change.

However, we may associate a local router to a specific area of the page with the router binding:

router binding

Where localRouter is the view model property containing the local router. Each local router has an associate url prefix: only urls starting with that prefix are processed by the router, all others link clicks are bubbled up till they reach another router binding. if no other local router is intercepted link clicks are processed by the main router if the url starts with '#', otherwise they cause a physical page change.

If a local router doesn't specify the fakeUrls option all its routing rules must start with '#'. Each url is split in its hash and server url parts. The hash part is processed by the routing rules, while the server url part must match the router prefix, otherwise the request is bubbled up. This limitation exists since local routers without the fakeUrls option must work only with urls that represent actual web resources to be shown into an area of the page. The same url inserted directly in the browser address bar is supposed to render the same resource.

If the fakeUrl option is specified the routing rules may have any format and the action binding always inserts the computed url in the data-action attribute. In fact in this case urls are used just as a way to trigger generic actions in the page, and doesn't have the meaning of actual web resources. Accordingly they are hidden to search engines by placing them in the data-action attribute instead of the href link attribute.

a local router is created with:

new ko.routing.router(true, baseUrl, fakeUrls);

The first argument set to true specifies that the router is local, baseUrl is the url prefix associated with the local router, and fakeUrls is the same fakeUrl option discussed above.

If an action binding isn't in the scope of any local router, its uses the main router rules to compute the url, otherwise it uses the innermost router it is in the scope of. We may specify a different router by adding it to the _router parameter:

action binding with property


routing rules parameters