angular-route-segment icon indicating copy to clipboard operation
angular-route-segment copied to clipboard

Add 'otherwise' wrapper method to route-segment

Open tminglei opened this issue 11 years ago • 8 comments

Hi @artch,

Your angular-route-segment not only added more features to angular-route, but also made the usage more elegant and simple!

This pull request is to add 'otherwise' wrapper method to route-segment. I know you want not to make unnecessary adjustment for angular-route. But this little change will make user codes more consistent, since you already adjusted the 'when' semantic.

The route-segment 'otherwise' can be used as below:

$routeSegmentProvider.
    when('/', 's0').
    when('/1/a', 's1.a').
    when('/2', 's2').
    ...
    otherwise('/').
    ...

Can you help review and give your suggestions?


Thanks for your awesome job! :D

tminglei avatar Jun 23 '14 14:06 tminglei

Hm, wouldn't we probably want to assign otherwise to a segment name, rather than to an URL?

artch avatar Jun 23 '14 15:06 artch

Well, assigning to an URL instead of a segment name, enable use to declare otherwise before or after other when declarations, instead of only after, since we obviously want to fetch redirectTo from it right now.

And assigning to a segment name seems not bring us any extra benefits. (Pls correct me if I was wrong.)

(p.s. the param route can also be a non-param function which return a URL. I just updated the comment.)

tminglei avatar Jun 23 '14 23:06 tminglei

Hi @artch @tminglei I'm also interested in an otherwise/catch-all route into the package. Another possibility to achieve this could be to encapsulate the $routeSegmentProvider.when and $routeProvider.otherwise into a single call which could be seen to simplify the scenario (?).

$routeSegmentProvider
  //normal route definitions
  .when('/', 'home')
  .when('/list/:category', 'list.category')

  //the catch-all route
  .else('/404', 'error')

The potential here is to define a segment as per usual and use the above syntax to specify the catch-all route for that segment.

    $routeSegmentProvider.else = function(route, name) {
      this.when(route, name);
      $routeProvider.otherwise({redirectTo: route});
      return this;
    };

I'm happy to create a PR if you think this approach is valuable, otherwise it's a discussion point.

@artch I guess it really depends on what you want to see, but I tend to agree with @tminglei's implementation if you wanted to introduce the $routeSegmentProvider.otherwise(). It would still be preferable for the method to accept the 'params' argument as per $routeProvider where the user can provide the segment name (not the route) to the redirectTo property.

qrb avatar Dec 22 '14 07:12 qrb

I got this error

$routeSegmentProvider.when(...).when(...).otherwise is not a function

where is the mistake?

sivascse avatar Jan 16 '15 19:01 sivascse

@sivascse there doesn't seem to be an otherwise function in $routeSegmentProvider yet. This is what we need too - we'd like to preserve the 404 URL and instead of redirecting to a specific page, just render a template with a controller. The syntax would be identical to the one in $routeProvider:

$routeSegmentProvider.otherwise({
  controller: 'notFound',
  templateUrl: 'common/not-found.html'
});

vdsabev avatar Feb 24 '15 17:02 vdsabev

I agree with @vdsabev. It would be great to display a template without having to redirect the user somewhere else, unless you decide to do so.

mcavallo avatar Mar 17 '15 16:03 mcavallo

What's the status here?

+1 for @vdsabev's proposal! Redirecting is not a clean solution...

andrenarchy avatar May 03 '15 19:05 andrenarchy

This seems like a simple patch that would be very useful. Can we get this accepted?

mischkl avatar Jul 07 '15 08:07 mischkl