new ObsRouter(patterns, options)
Mutable observable abstraction of url as route with parameters
Parameters:
Name |
Type |
Argument |
Description |
patterns |
Object
|
|
Pathname patterns keyed by route names. |
options |
Object
|
<optional>
|
Options
Properties
Name |
Type |
Argument |
Default |
Description |
initialEmit |
Boolean
|
<optional>
|
false
|
If true, events will be emitted after nextTick, unless emitted earlier due to changes, ensuring that events are emitted at least once. |
bindToWindow |
Boolean
|
<optional>
|
true
|
Bind to document location if running in browser |
url |
string
|
<optional>
|
""
|
Initial url. If binding to document loctation then this is ignored. |
|
- Source:
Example
var router = new ObsRouter({
home: '/',
blog: '/blog(/tag/:tag)(/:slug)',
contact: '/contact'
}, {
initialEmit: true,
bindToWindow: false,
url: '/?foo=bar'
});
console.log(router.url, router.name, router.params, router.routes);
// -> '/?foo=bar' 'home' {foo: 'bar'} {home: {foo: 'bar'}, blog: null, contact: null}
-
<static> routeToUrl(patterns, name, params) → {string}
-
Converts a route (name & params) to a url, given patterns
Parameters:
Name |
Type |
Argument |
Default |
Description |
patterns |
Object
|
|
|
Pathname patterns keyed by route names |
name |
string
|
|
|
Route name |
params |
Object
|
<optional>
|
{}
|
Route parameters |
- Source:
Returns:
Url
-
Type
-
string
-
<static> urlToRoute(patterns, url, params) → {string|null}
-
Converts a url to a name & params, given patterns
The optional params argument is used to pass back the arguments, and only the name is `return`ed.
Parameters:
Name |
Type |
Argument |
Description |
patterns |
Object
|
|
Pathname patterns keyed by route names |
url |
string
|
|
Url to convert. |
params |
Object
|
<optional>
|
Parameters object to populate. |
- Source:
Returns:
Route name or null if no route matched
-
Type
-
string
|
null
-
destroy()
-
Cleanup method to be called when you're done with your ObsRouter instance.
- Source:
-
pushRoute(name, params, extend_params)
-
Uses History.pushState to change the route name + params to new route name + params and updates url + routes
Pushing rather than replacing means the browser's user can get to the previous state with the back button.
Parameters:
Name |
Type |
Argument |
Default |
Description |
name |
string
|
<optional>
|
this.name
|
The new route name |
params |
Object
|
<optional>
|
{}
|
The new parameters |
extend_params |
Boolean
|
<optional>
|
false
|
Extend parameters rather than replacing them. |
- Source:
-
pushUrl(url)
-
Uses History.pushState to change url to new url and updates route name + params + routes
Pushing rather than replacing means the browser's user can get to the previous state with the back button.
Parameters:
Name |
Type |
Description |
url |
string
|
The new url |
- Source:
-
replaceRoute(name, params, extend_params)
-
Uses History.replaceState to change the url to new url and updates route name + params + routes
Replacing rather than pushing means the browser's user can not get to the previous state with the back button.
Parameters:
Name |
Type |
Argument |
Default |
Description |
name |
string
|
<optional>
|
this.name
|
The new route name |
params |
Object
|
<optional>
|
{}
|
The new parameters |
extend_params |
Boolean
|
<optional>
|
false
|
Extend parameters rather than replacing them. |
- Source:
-
replaceUrl(url)
-
Uses History.replaceState to change url to new url and updates route name + params + routes
Replacing rather than pushing means the browser's user can not get to the previous state with the back button.
Parameters:
Name |
Type |
Description |
url |
string
|
The new url |
- Source:
-
routeToUrl(name, params, extend_params) → {string}
-
Converts a route (name & params) to a url
Parameters:
Name |
Type |
Argument |
Default |
Description |
name |
string
|
<optional>
|
this.name
|
Route name |
params |
Object
|
<optional>
|
{}
|
Route parameters |
extend_params |
Boolean
|
<optional>
|
false
|
Extend parameters rather than replacing them. |
- Source:
Returns:
Url
-
Type
-
string
Example
// simple example
var url = router.routeToUrl('blog', {slug: 'Why_I_love_Russian_girls', page: 82});
console.log(url); // -> /blog/Why_I_love_Russian_girls?page=82
// cool example using extend_params=true
console.log(router.params); // -> {tags: 'sexy'}
var url = router.routeToUrl('blog', {filter: 'graphic'}, true);
console.log(url); // -> '/blog/tags/sexy?filter=graphic'
-
urlToRoute(url, params) → {string|null}
-
Converts a url to a name & params.
The optional params argument is used to pass back the arguments, and only the name is `return`ed.
Parameters:
Name |
Type |
Argument |
Description |
url |
string
|
|
Url to convert. |
params |
Object
|
<optional>
|
Parameters object to populate. |
- Source:
Returns:
Route name or null if no route matched
-
Type
-
string
|
null
Example
var route_params = {};
var route_name = router.urlToRoute('/blog', route_params);
console.log(route_name, route_params);
// -> blog {tag: undefined, slug: undefined}