W3cubDocs

/Dojo

dojo/router/RouterBase

Summary

A module that allows one to easily map hash-based structures into callbacks. The router module is a singleton, offering one central point for all registrations of this type.

Usage

var foo = new RouterBase(kwArgs);

dojo/router/RouterBase

Parameter Type Description
kwArgs undefined

See the dojo/router/RouterBase reference documentation for more information.

Examples

Example 1

var router = new RouterBase({});
router.register("/widgets/:id", function(evt){
    // If "/widgets/3" was matched,
    // evt.params.id === "3"
    xhr.get({
        url: "/some/path/" + evt.params.id,
        load: function(data){
            // ...
        }
    });
});

Properties

globMatch

Defined by: dojo/router/RouterBase

globReplacement

Defined by: dojo/router/RouterBase

idMatch

Defined by: dojo/router/RouterBase

idReplacement

Defined by: dojo/router/RouterBase

Methods

destroy()

Defined by dojo/router/RouterBase

go(path,replace)

Defined by dojo/router/RouterBase

A simple pass-through to make changing the hash easy, without having to require dojo/hash directly. It also synchronously fires off any routes that match.

Parameter Type Description
path undefined
replace undefined

Returns: boolean | undefined

Examples

Example 1

router.go("/foo/bar");

register(route,callback)

Defined by dojo/router/RouterBase

Registers a route to a handling callback

Given either a string or a regular expression, the router will monitor the page's hash and respond to changes that match the string or regex as provided.

When provided a regex for the route:

  • Matching is performed, and the resulting capture groups are passed through to the callback as an array.

When provided a string for the route:

  • The string is parsed as a URL-like structure, like "/foo/bar"
  • If any portions of that URL are prefixed with a colon (:), they will be parsed out and provided to the callback as properties of an object.
  • If the last piece of the URL-like structure is prefixed with a star (*) instead of a colon, it will be replaced in the resulting regex with a greedy (.+) match and anything remaining on the hash will be provided as a property on the object passed into the callback. Think of it like a basic means of globbing the end of a route.
Parameter Type Description
route String | RegExp

A string or regular expression which will be used when monitoring hash changes.

callback Function

When the hash matches a pattern as described in the route, this callback will be executed. It will receive an event object that will have several properties:

  • params: Either an array or object of properties pulled from the new hash
  • oldPath: The hash in its state before the change
  • newPath: The new hash being shifted to
  • preventDefault: A method that will stop hash changes from being actually applied to the active hash. This only works if the hash change was initiated using router.go, as changes initiated more directly to the location.hash property will already be in place
  • stopImmediatePropagation: When called, will stop any further bound callbacks on this particular route from being executed. If two distinct routes are bound that are different, but both happen to match the current hash in some way, this will not keep other routes from receiving notice of the change.

Returns: Object | undefined

A plain JavaScript object to be used as a handle for either removing this specific callback's registration, as well as to add new callbacks with the same route initially used.

Examples

Example 1

router.register("/foo/:bar/*baz", function(object){
    // If the hash was "/foo/abc/def/ghi",
    // object.bar === "abc"
    // object.baz === "def/ghi"
});

registerBefore(route,callback)

Defined by dojo/router/RouterBase

Registers a route to a handling callback, except before any previously registered callbacks

Much like the register method, registerBefore allows us to register route callbacks to happen before any previously registered callbacks. See the documentation for register for more details and examples.

Parameter Type Description
route String | RegExp
callback Function

Returns: undefined

startup(defaultPath)

Defined by dojo/router/RouterBase

This method must be called to activate the router. Until startup is called, no hash changes will trigger route callbacks.

Parameter Type Description
defaultPath undefined

© 2005–2017 JS Foundation
Licensed under the AFL 2.1 and BSD 3-Clause licenses.
http://dojotoolkit.org/api/1.10/dojo/router/RouterBase.html