Router
Router handles URL routing in Viking applications.
The Router maps URLs to controllers and actions, allowing for clean, RESTful URLs. It manages browser history and provides navigation methods to move between application states without full page reloads.
Router Events
| Event | Arguments | Description |
|---|---|---|
| beforeNavigation | url | Triggered when navigation in the app is about to happen |
| afterNavigation | url | Triggered when navigation in the app has completed |
Constructor
new Router(application)
Instance Properties
Object containing references to objects this instance is listening to
Regular expression for escaping special characters in routes
Object containing all event listeners
Unique identifier for this event bus instance
Regular expression for matching named parameters
Regular expression for matching optional parameters
Static routes configuration
Regular expression for matching splat parameters
Instance Methods
Bind an event to a callback function. Passing "*" will bind
the callback to all events fired.
If the options.once is true, the event will only be triggered a single time. After the first time the callback is invoked, its listener will be removed. If multiple events are passed in as an array, the handler will fire once for each event, not once for a combination of all events.
Parameters
name
:
string
|
Array.<string>
|
object
Event name(s) or object of event/callback pairs
callback
:
function
|
object
Callback function or options object if name is an object
options
:
object
optionalEvent options
once
:
boolean
optionalWhether the event should only trigger once
context
:
object
optionalThe context to bind the event to
listener
:
object
optionalThe listener object
Returns
EventBus
Returns this for chaining
Tell this object to listen to an event in another object, while keeping track of what it's listening to for easier unbinding later.
Parameters
obj
:
object
|
NodeList
The object to listen to events on
name
:
string
|
Array.<string>
|
object
Event name(s) or object of event/callback pairs
callback
:
function
|
object
Callback function or options object if name is an object
options
:
object
optionalEvent options
once
:
boolean
optionalWhether the event should only trigger once
context
:
object
optionalThe context to bind the event to
Returns
EventBus
Returns this for chaining
Trigger one or many events, firing all bound callbacks. Callbacks are
passed the same arguments as dispatchEvent is, apart from the event name
(unless you're listening on "*", which will cause your callback to
receive the true name of the event as the first argument).
Parameters
name
:
string
|
Array.<string>
Event name or array of event names
args
:
*
Arguments to pass to the event handlers
Returns
EventBus
Returns this for chaining
Get the current URL query string
Returns
string
The query string portion of the URL
Find the handler for a given path
Parameters
path
:
string
The path to find a handler for
Returns
RouterHandler
|
undefined
The handler for the path, or undefined if not found
Initialize is an empty function by default. Override it with your own initialization logic if needed.
Load the current URL and execute the matching handler
This method is called on popstate events and when navigating programmatically
Remove one or many callbacks that match the arguments. If called with no arguments, removes all callbacks for all events.
Parameters
name
:
string
|
Array.<string>
|
object
|
null
optionalEvent name(s) or object of event/callback pairs
callback
:
function
|
object
|
null
optionalCallback function or options object if name is an object
options
:
object
optionalEvent options
once
:
boolean
optionalMatch only events set to trigger once
context
:
object
optionalMatch only events bound to this context
listener
:
object
optionalMatch only events bound by this listener
Returns
EventBus
Returns this for chaining
Tell this object to stop listening to either specific events or to every object it's currently listening to.
Parameters
obj
:
object
|
NodeList
optionalThe object to stop listening to; if omitted, stops listening to all objects
name
:
string
|
Array.<string>
|
object
optionalEvent name(s) or object of event/callback pairs
callback
:
function
optionalCallback function to remove
options
:
object
optionalEvent options
once
:
boolean
optionalMatch only events set to trigger once
context
:
object
optionalMatch only events bound to this context
Returns
EventBus
Returns this for chaining
Define a route that maps a URL pattern to a controller action or callback function
Parameters
route
:
string
|
RegExp
The route pattern to match
name
:
string
|
Object
|
function
optionalThe name of the route, controller#action string, or handler function
callback
:
function
optionalThe callback function to execute when the route matches
Returns
Viking.Router
Returns this for chaining
Convert a route string pattern to a regular expression
The router uses the following syntax for defining routes:
:name- Named parameter*splat- Splat parameter(...)- Optional part
Parameters
route
:
string
The route pattern to convert
Returns
RegExp
A regular expression that matches the route pattern
start()
Start the router, loading the current URL
stop()
Stop the router, removing event listeners
