justinfagnani / route

A client + server routing library for Dart
BSD 3-Clause "New" or "Revised" License
114 stars 40 forks source link

Handle URL parameters #38

Open polux opened 11 years ago

polux commented 11 years ago

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.

sethladd commented 11 years ago

/sub

justinfagnani commented 11 years ago

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.

polux commented 11 years ago

Sounds perfect!

polux commented 11 years ago

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.

pavelgj commented 11 years ago

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...

polux commented 11 years ago

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