new Router(options, data)
Router manager class with built-in HistoryManager. Add routes to router and let Router to handle those routes along with HistoryManager.
Parameters:
Name | Type | Argument | Description |
---|---|---|---|
options |
Object |
<optional> |
Class options. |
data |
* |
<optional> |
Class data |
- Source:
Extends
Methods
-
addRoute(route, callback)
-
Add route with callback function to handle it.
Parameters:
Name Type Description route
string Route path.
callback
function Callback to handle route action.
- Source:
-
back()
-
Routes back on the page. If there is no previous route, it will do nothing.
- Source:
-
<private> createRegexRoute_(route) → {RegExp|undefined}
-
Creates a regex for route to match actual page link with our routes. This method create a regex like
\/activity\/post\/(\w+)\/comment\/(\w+)$
to match this route/activity/post/1/comment/2
. Here is the regex101 https://www.regex101.com/r/wX2kB6/1Parameters:
Name Type Description route
string Actual page route.
- Source:
Returns:
RegExp to match page URL.
- Type
- RegExp | undefined
-
destroy()
-
Destroy the router instance. If you want to create a new router instance for some reason, you have to destroy previously created instances. Otherwise HistoryManager events will be triggered multiple times.
- Source:
-
emit(eventName, data)
-
Dispatches an event and calls all listeners listening for events of this event. See also goog.events.Listenable.prototype.dispatchEvent
Parameters:
Name Type Argument Description eventName
string Name of the event.
data
* <optional>
Data which will passed to listeners
- Inherited From:
- Source:
-
forward()
-
Routes forward on the page. If there is no next route, it will do nothing.
- Source:
-
freeze()
-
Freezes object to prevent adding new properties, updating or deleting existing properties.
- Inherited From:
- Source:
-
getData() → {*}
-
Returns class data.
- Inherited From:
- Source:
Returns:
Class data.
- Type
- *
-
getHistoryManager() → {spark.core.HistoryManager}
-
Returns spark.core.HistoryManager instance.
- Source:
Returns:
HistoryManager instance of the Router.
-
getOption() → {*}
-
Returns value of a key in options object.
- Inherited From:
- Source:
Returns:
Value of key or null.
- Type
- *
-
getOptions() → {Object}
-
Returns all options object.
- Inherited From:
- Source:
Returns:
Options object.
- Type
- Object
-
getRoutes() → {Object}
-
Returns the currently added routes.
- Source:
Returns:
Routes object.
- Type
- Object
-
getUid() → {string}
-
Return unique id.
- Inherited From:
- Source:
Returns:
Unique id of this component.
- Type
- string
-
<private> handleRoute_()
-
Execute callback function for particular route path.
- Source:
-
init()
-
Handle route for current page path. This is useful for handling the initial route of the page when page reload. This method should be called by user because Router doesn't actually know when all routes are added.
TODO: Rename this method.
- Source:
-
isDestroyed() → {boolean}
-
Returns the object's destroy state.
- Inherited From:
- Source:
Returns:
Whether the object is destroyed or not.
- Type
- boolean
-
off(eventName, callback) → {boolean}
-
Removes an event listener which was added with @on or @once. See also goog.events.Listenable.prototype.unlisten
Parameters:
Name Type Description eventName
string Name of the event.
callback
function Callback function for the event.
- Inherited From:
- Source:
Returns:
Whether any listener was removed.
- Type
- boolean
-
on(eventName, callback) → {goog.events.ListenableKey|number}
-
Adds an event listener. See also goog.events.Listenable::listen
Parameters:
Name Type Description eventName
string Name of the event.
callback
function Callback function for the event.
- Inherited From:
- Source:
Returns:
Unique key for the listener.
- Type
- goog.events.ListenableKey | number
-
once(eventName, callback) → {goog.events.ListenableKey|number}
-
Adds an event listener that is removed automatically after the listener fired once. See also goog.events.Listenable.prototype.listenOnce
Parameters:
Name Type Description eventName
string Name of the event.
callback
function Callback function for the event.
- Inherited From:
- Source:
Returns:
Unique key for the listener.
- Type
- goog.events.ListenableKey | number
-
route(path)
-
Sets HistoryManager's token to update URL. When history token is changed HistoryManager will emit Navigated event. It's handled in here and it will call the correct callback for that route.
Parameters:
Name Type Description path
string Path to change the active route.
- Source:
-
setData(data)
-
Sets data of this class.
Parameters:
Name Type Description data
* Data passed to class.
- Inherited From:
- Source:
-
setOptions(options)
-
Sets options object of this class.
Parameters:
Name Type Description options
Object Options object.
- Inherited From:
- Source:
-
<private> setUid_()
-
Sets uid.
- Inherited From:
- Source: