Feature onmatch route param (#2371)

* added route param in onmatch

* fix docs

* included test

* docs fix
This commit is contained in:
Yogesh Khatri 2019-02-07 15:42:53 +05:30 committed by Isiah Meadows
parent 6f8f011b88
commit a9172f1129
3 changed files with 15 additions and 9 deletions

View file

@ -23,7 +23,7 @@ module.exports = function($window, redrawService) {
if (path !== defaultRoute) routeService.setPath(defaultRoute, null, {replace: true}) if (path !== defaultRoute) routeService.setPath(defaultRoute, null, {replace: true})
else throw new Error("Could not resolve default route " + defaultRoute) else throw new Error("Could not resolve default route " + defaultRoute)
} }
routeService.defineRoutes(routes, function(payload, params, path) { routeService.defineRoutes(routes, function(payload, params, path, route) {
var update = lastUpdate = function(routeResolver, comp) { var update = lastUpdate = function(routeResolver, comp) {
if (update !== lastUpdate) return if (update !== lastUpdate) return
component = comp != null && (typeof comp.view === "function" || typeof comp === "function")? comp : "div" component = comp != null && (typeof comp.view === "function" || typeof comp === "function")? comp : "div"
@ -34,7 +34,7 @@ module.exports = function($window, redrawService) {
if (payload.view || typeof payload === "function") update({}, payload) if (payload.view || typeof payload === "function") update({}, payload)
else { else {
if (payload.onmatch) { if (payload.onmatch) {
Promise.resolve(payload.onmatch(params, path)).then(function(resolved) { Promise.resolve(payload.onmatch(params, path, route)).then(function(resolved) {
update(payload, resolved) update(payload, resolved)
}, bail) }, bail)
} }

View file

@ -321,11 +321,12 @@ o.spec("route", function() {
} }
var resolver = { var resolver = {
onmatch: function(args, requestedPath) { onmatch: function(args, requestedPath, route) {
matchCount++ matchCount++
o(args.id).equals("abc") o(args.id).equals("abc")
o(requestedPath).equals("/abc") o(requestedPath).equals("/abc")
o(route).equals("/:id")
o(this).equals(resolver) o(this).equals(resolver)
return Component return Component
}, },
@ -362,11 +363,12 @@ o.spec("route", function() {
} }
var resolver = { var resolver = {
onmatch: function(args, requestedPath) { onmatch: function(args, requestedPath, route) {
matchCount++ matchCount++
o(args.id).equals("abc") o(args.id).equals("abc")
o(requestedPath).equals("/abc") o(requestedPath).equals("/abc")
o(route).equals("/:id")
o(this).equals(resolver) o(this).equals(resolver)
return Promise.resolve(Component) return Promise.resolve(Component)
}, },
@ -398,11 +400,12 @@ o.spec("route", function() {
var renderCount = 0 var renderCount = 0
var resolver = { var resolver = {
onmatch: function(args, requestedPath) { onmatch: function(args, requestedPath, route) {
matchCount++ matchCount++
o(args.id).equals("abc") o(args.id).equals("abc")
o(requestedPath).equals("/abc") o(requestedPath).equals("/abc")
o(route).equals("/:id")
o(this).equals(resolver) o(this).equals(resolver)
return Promise.resolve() return Promise.resolve()
}, },
@ -434,11 +437,12 @@ o.spec("route", function() {
var renderCount = 0 var renderCount = 0
var resolver = { var resolver = {
onmatch: function(args, requestedPath) { onmatch: function(args, requestedPath, route) {
matchCount++ matchCount++
o(args.id).equals("abc") o(args.id).equals("abc")
o(requestedPath).equals("/abc") o(requestedPath).equals("/abc")
o(route).equals("/:id")
o(this).equals(resolver) o(this).equals(resolver)
return Promise.resolve([]) return Promise.resolve([])
}, },
@ -508,11 +512,12 @@ o.spec("route", function() {
$window.location.href = prefix + "/abc" $window.location.href = prefix + "/abc"
route(root, "/abc", { route(root, "/abc", {
"/:id" : { "/:id" : {
onmatch: function(args, requestedPath) { onmatch: function(args, requestedPath, route) {
matchCount++ matchCount++
o(args.id).equals("abc") o(args.id).equals("abc")
o(requestedPath).equals("/abc") o(requestedPath).equals("/abc")
o(route).equals("/:id")
return Component return Component
}, },

View file

@ -176,12 +176,13 @@ This method also allows you to asynchronously define what component will be rend
For more information on `onmatch`, see the [advanced component resolution](#advanced-component-resolution) section For more information on `onmatch`, see the [advanced component resolution](#advanced-component-resolution) section
`routeResolver.onmatch(args, requestedPath)` `routeResolver.onmatch(args, requestedPath, route)`
Argument | Type | Description Argument | Type | Description
--------------- | ---------------------------------------- | --- --------------- | ---------------------------------------- | ---
`args` | `Object` | The [routing parameters](#routing-parameters) `args` | `Object` | The [routing parameters](#routing-parameters)
`requestedPath` | `String` | The router path requested by the last routing action, including interpolated routing parameter values, but without the prefix. When `onmatch` is called, the resolution for this path is not complete and `m.route.get()` still returns the previous path. `requestedPath` | `String` | The router path requested by the last routing action, including interpolated routing parameter values, but without the prefix. When `onmatch` is called, the resolution for this path is not complete and `m.route.get()` still returns the previous path.
`route` | `String` | The router path requested by the last routing action, excluding interpolated routing parameter values
**returns** | `Component|Promise<Component>|undefined` | Returns a component or a promise that resolves to a component **returns** | `Component|Promise<Component>|undefined` | Returns a component or a promise that resolves to a component
If `onmatch` returns a component or a promise that resolves to a component, this component is used as the `vnode.tag` for the first argument in the RouteResolver's `render` method. Otherwise, `vnode.tag` is set to `"div"`. Similarly, if the `onmatch` method is omitted, `vnode.tag` is also `"div"`. If `onmatch` returns a component or a promise that resolves to a component, this component is used as the `vnode.tag` for the first argument in the RouteResolver's `render` method. Otherwise, `vnode.tag` is set to `"div"`. Similarly, if the `onmatch` method is omitted, `vnode.tag` is also `"div"`.
@ -429,7 +430,7 @@ Instead of mapping a component to a route, you can specify a RouteResolver objec
```javascript ```javascript
m.route(document.body, "/", { m.route(document.body, "/", {
"/": { "/": {
onmatch: function(args, requestedPath) { onmatch: function(args, requestedPath, route) {
return Home return Home
}, },
render: function(vnode) { render: function(vnode) {