route icon indicating copy to clipboard operation
route copied to clipboard

Handle URL parameters

Open polux opened this issue 11 years ago • 6 comments

Would it make sense to support URL parameters?

I've been using route with an app that used to do routing manually, sometimes using URL parameters. I want to maintain compatibility with the already published URLs for this app so I have to hack around routes to get it done (by capturing the URL parameters).

It would be nice to have native support for it.

polux avatar Jun 24 '13 07:06 polux

/sub

sethladd avatar Jul 01 '13 18:07 sethladd

The hierarchical branch has some query parameter support. We're working on a new version of that branch and will bring it into master soon.

Some notes on how this might work:

Right now my thinking is that Routers will allow for more than one simultaneous match, so that you could have independently controlled parts of the UI. Routes will then match against pretty arbitrary application state, including the current URI path, fragment, query parameters, and possibly other any other state.

For something like query parameters, there may be cases where you want to match against a key/value pair regardless of other state like the path, and there may be cases where you only want to match a key/value pair when already in some other state. The way this dependency will be expressed is via the query matching route's location in the route hierarchy. If it's at the top level, it's independent of what state the rest of the Router is in. If it's mounted under some path Routes, then it only matches if it's ancestors match as well.

justinfagnani avatar Jul 05 '13 20:07 justinfagnani

Sounds perfect!

polux avatar Jul 05 '13 20:07 polux

The hierarchical branch has some query parameter support.

Could you please point me to the line in the hierarchical branch that handles them? (Or even better an example.) I see how URLs like /foo/:x/bar are supported (via parameters in RouteEvent) but I couldn't find a way of getting my hands on x: v in /foo?x=v.

polux avatar Nov 04 '13 22:11 polux

Currently, in hierarchical branch, query parameters are namespaced -- they must be prefixed with the route name. For example. for URL: /foo/bar?bar.baz=1234

router.root
  ..addRoute(name: 'foo', path: '/foo', mount: (r) => r
    ..addRoute(name: 'bar', path: '/bar'))
expect(router.root.getRoute('foo.bar').parameters['baz'], '1234');

This API is a... work in progress...

pavelgj avatar Nov 04 '13 22:11 pavelgj

Thanks for the explanation, good to know! I guess I'll stick with the current solution until there's support for arbitrary parameter names.

polux avatar Nov 04 '13 23:11 polux