new Router(options)
The Router factory / class, responsible for creating a Router.
Parameters:
Name | Type | Description |
---|---|---|
options |
Object |
- To Do:
-
- implement defaultRoute, notFoundRoute, errorRoute (for controller errors)
Properties:
Name | Type | Argument | Default | Description |
---|---|---|---|---|
controllers |
Object.<Object.<function()>> | Hashmap containing Controllers | ||
policies |
Object.<function()> | Hashmap containing policies | ||
routes |
Object.<Object> | Hashmap containing routes | ||
success |
function | Function that gets executed when a route has successfully executed | ||
fail |
function | Function that gets executed when a route has failed to execute successfully | ||
sync |
function | Function that gets executed when a controller syncs | ||
anchorSelector |
String |
<optional> |
'a[href^="/"]:not([href^="//"])' | jQuery selector that represents all anchors that should be event.preventDefault()'ed and use the router to navigate instead |
pushState |
Boolean | Grapnel pushState option | ||
root |
Boolean | Grapnel root option | ||
env |
Boolean | Grapnel env option | ||
mode |
Boolean | Grapnel mode option | ||
hashBang |
Boolean | Grapnel hashBang option |
Example
const router = Router({ pushState: true, success(route, data) { // the route and data resolved from the Controller }, fail(route, data) { // the failed route and data from policy/controller that failed data.reason; // 'policy' or 'controller' data.data; // data policy/controlller rejected with }, sync(route, data) { // controller sync happened for the route passed in with data passed in }, routes: { '/testReject': { policies: ['test'], controller: 'test.test' } }, '/testResolve': { policies: [], controller: 'test.test' } }, policies: { test: function() { return Promise.reject(); } }, controllers: { test: { test: function () { return { test: true } } } } });
Members
-
<static> defaults :Object
-
Default properties for objects passed into the Router factory, gets merged (deeply) with the options passed in.
Type:
- Object
Properties:
Name Type Argument Default Description policies
Object.<function()> <optional>
{} Policies specified as a hashmap controllers
Object.<Object.<function()>> <optional>
{} Controllers specified as a hashmap -
<static> policyExecutor :PolicyExecutor
-
The
PolicyExecutor
instance allRouter
s use, this may be overriddenType:
- PolicyExecutor
-
<static> routeDefaults :Object
-
Default properties for route objects, gets merged (deeply) with the routes.
Type:
- Object
- Default Value:
-
- {}
-
grapnel :Object
-
An instance of the Grapnel router, responsible for actual routing
Type:
- Object
-
options :Object
-
The options as passed into the factory
Type:
- Object
Methods
-
navigate(url, options)
-
Navigates to a url
Parameters:
Name Type Argument Description url
String Url to navigate to options
Object <optional>
Object containing the properties listed below - To Do:
-
- add a data param so that urls an contains splats that will be filled with the data
Properties:
Name Type Argument Default Description trigger
Boolean <optional>
true Indicates a route event should be triggered, so the route gets executed replace
Boolean <optional>
false Indicates the history item should be replaced Example
// regular navigate router.navigate('/user/3'); // replace the history item router.navigate('/user/6', { replace: true }); // dont trigger event, route won't be handled router.navigate('/user/6', { trigger: false });
-
policy(policy, data)
-
Executes one or more policies.
Parameters:
Name Type Description policy
String | Array.<String> Policy / Policies to execute data
Object 'Request' data Example
router.policy('isLoggedIn') .then(...) router.policy(['isLoggedIn', 'isLoggedInUser'], userModel) .then(...)
-
redirect(url)
-
Redirects to a url, replaces the current history item.
Parameters:
Name Type Description url
String Url to redirect to - To Do:
-
- add a data param so that urls an contains splats that will be filled with the data
Example
router.redirect('/user/5');
-
reload()
-
Reloads the current page without actually reloading
Example
route.reload();