Is it possible to have (an undefined number of) optional segments in the Ember router?

34 Views Asked by At

I would like to collect extra segments from the Ember router in an array. Here is a made-up path to illustrate what I mean:

this.route('group', {path: 'group/:group_id(/:segments[])*'}, function() {

Is it possible to use a request like this:

GET /group/123/some/path/segments

And have them collected in an array?

group.id = 123
segments = ['some', 'path', 'segments']

Or is there any way to define optional segments, so I can just add many and collect them manually?

1

There are 1 best solutions below

1
mistahenry On BEST ANSWER

Under the hood, the router is using route-recognizer to determine the routes. There is the notion of star-segments

router.add([{ path: "/pages/*path", handler: page }]);

result = router.recognize("/pages/hello/world");
result === [{ handler: page, params: { path: "hello/world" } }];

This seems like what you're looking for

As a side note, this is my usual 404 approach:

Router.map(function() {
  ...very last route
  this.route('not-found', { path: "/*path"});
});

routes/not-found.js:

export default Route.extend({
    model(params){
        return params.path
    }
});

not-found.hbs

404: /{{model}} not found!

such that /foo/bar yields: 404: /foo/bar not found!