Added support of redirectTo instruction at segment params.
Useful for managing external and internal redirects within a route definitions
Syntax:
$routeSegmentProvider
.when('/section', 's1')
.when('/section/index', 's1.idx')
.segment('section', {
redirectTo: 'section/index'
})
.within()
.segment('idx', {
templateUrl: '/views/segment/idx.html'
});
Great work! Right now I'm creating a separate controller for every route I want to redirect to. It'd be really nice to see this merged. But I think you might have to add tests for this to happen...
@wingedfox Your provided example could be easily rewrited like this:
$routeSegmentProvider
.when('/section', 's1.idx') // you can use deep-level segment here
.when('/section/index', 's1.idx')
.segment('section', {
template: '<div app-view-segment="1"></div>'
})
.within()
.segment('idx', {
templateUrl: '/views/segment/idx.html'
});
There is no need in separate redirectTo functionality for such a case. Also, you can use $routeProvider's redirecting as well:
$routeProvider.when('/invalidUrl', {redirectTo: '/validUrl'});
$routeSegmentProvider.when('/validUrl', 'segment')
.segment('segment', ...);
But your latter commits look really interesting. Could you please provide tests on it?
@artch, even if I can buy a piglet, I prefer not to do that :) In your example you'll have all the relative links broken. Redirects in my example are used for hiding gaps in the navigation tree, I prefer to cover them by local redirects instead of a global ones. E.g. at a given time I have a mapping
/ -> /a
/a -> /a/b
/a/b -> /a/b/c
for sure this does not mean that the following will be correct at any given moment
/ -> /a/b/c
/a -> /a/b/c
when a root page is added inner redirects will remain intact.
I'll try to add the tests, but I don't work on that project anymore so first I'll have to remember what's going on there. Anyway, it will take a place not before the mid-january, because I'll be on a vacation.
A bit unclear for me. You wrote:
$routeSegmentProvider
.when('/section', 's1')
.when('/section/index', 's1.idx')
.segment('section', {
redirectTo: 'section/index'
})
This piece of configuration is identical to:
$routeSegmentProvider
.when('/section', 's1.idx')
.when('/section/index', 's1.idx')
Isn't it?
Following your way link from template
<a href="b" />
with /section in the address bar it will point to /b with /section/index in the address bar it will point to /section/b
When using redirects, you'll never see /section in the address bar, you always will be redirected to /section/index.
OK, why not to use built-in angular $routeProvider.when('/section', {redirectTo: '/section/index'}) for this case? I really see no reason for implementing redirects at segments level of abstraction.
For me, this is the vital case. Without this feature I'll have the dozens of
$routeProvider.when('/section', {redirectTo: '/section/index'})
instructions, placed somewhere out of context, where the section is defined. Take a look:
$routeProvider.when('/', {redirectTo: '/reports/market/au/slices'})
$routeProvider.when('/reports', {redirectTo: '/reports/market/au/slices'})
$routeProvider.when('/reports/market', {redirectTo: '/reports/market/au/slices'})
$routeProvider.when('/reports/market/au', {redirectTo: '/reports/market/au/slices'})
$routeProvider.when('/reports/market/value', {redirectTo: '/reports/market/value/sum'})
Here I have to change 3 lines to make /reports/market/value/sum default report. With per-segment redirects I'll have to change only one line. I have a script for building the routing table for the reports and I'll prefer to generate as few lines as possible.
I see. How do you assign segments to URLs in the case of these 5 routes?