Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cf80ebf

Browse files
committed
nest views: ellipsees
1 parent 7ae82a1 commit cf80ebf

File tree

4 files changed

+56
-58
lines changed

4 files changed

+56
-58
lines changed

src/ng/route.js

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ function $RouteProvider(){
3636
var key = scopeKey(scope);
3737
delete scopedRoutes[key];
3838
}
39+
40+
function scoped(scope) {
41+
var key = scopeKey(scope);
42+
if (key in scopedRoutes)
43+
return scopedRoutes[key];
44+
else {
45+
return scoped(scope.$parent);
46+
}
47+
}
3948

4049
// the root routes have null scope
4150
addScope(null);
@@ -98,23 +107,28 @@ function $RouteProvider(){
98107
* @description
99108
* Adds a new route definition to the `$route` service.
100109
*/
110+
var trailingRegex = /^.*(\.\.\.|\*)$/g;
101111
function when(scope, path, route, opts) {
102112

113+
if (this.base && isString(path) && path[0] !== '/') {
114+
path = this.base + path;
115+
}
116+
103117
pushRoute(scope, extend(
104-
{reloadOnSearch: true, reloadOnParams: true},
118+
{reloadOnSearch: true},
105119
route,
106-
path && pathRegExp(path, extend(options, opts))
120+
path && pathRegExp(path, extend({}, options, opts))
107121
));
108122

109123
// create redirection for trailing slashes
110-
if (path) {
111-
var redirectPath = (path[path.length-1] == '/')
124+
if (path && !trailingRegex.exec(path)) {
125+
var redirectPath = (path[path.length-1] === '/')
112126
? path.substr(0, path.length-1)
113127
: path +'/';
114128

115129
pushRoute(scope, extend(
116130
{redirectTo: path},
117-
pathRegExp(redirectPath, extend(options, opts))
131+
pathRegExp(redirectPath, extend({}, options, opts))
118132
));
119133
}
120134

@@ -137,7 +151,6 @@ function $RouteProvider(){
137151

138152
path = path
139153
.replace(/([\\\(\)\^\$])/g, "\\$1")
140-
.replace(/\.\.\.$/g, opts.noEllipsis ? '': '(?:.*)')
141154
.concat(strict ? '' : '/?')
142155
.replace(/\/\(/g, '(?:/')
143156
.replace(/(\/)?(\.)?:(\w+)(?:(\(.*?\)))?(\?)?(\*)?/g, function(_, slash, format, key, capture, optional, star){
@@ -152,9 +165,13 @@ function $RouteProvider(){
152165
+ (star ? '(/*)?' : '');
153166
})
154167
.replace(/([\/.])/g, '\\$1')
155-
.replace(/\*/g, '(.*)');
168+
.replace(/\*/g, '(.*)')
169+
.replace(/\\.\\.\\.$/g, function(a, b) {
170+
ret.ellipsis = true;
171+
return ''
172+
});
156173

157-
ret.regexp = new RegExp('^' + path + '$', sensitive ? '' : 'i')
174+
ret.regexp = new RegExp('^' + path + (ret.ellipsis ? '' : '$'), sensitive ? '' : 'i');
158175
return ret
159176
}
160177

@@ -175,7 +192,7 @@ function $RouteProvider(){
175192
routes = scopedRoutes[key].routes;
176193

177194
routes[null] = extend(
178-
{reloadOnSearch: true, reloadOnParams: true},
195+
{reloadOnSearch: true},
179196
params);
180197
return this;
181198
}
@@ -199,26 +216,6 @@ function $RouteProvider(){
199216
}
200217

201218

202-
203-
/*
204-
* @ngdoc method
205-
* @name ng.$routeProvider#options
206-
* @methodOf ng$routeProvider
207-
*
208-
* @description
209-
* Options fore route matching.
210-
* - `sensitive` enable case-sensitive routes
211-
* - `strict` disable strict matching for trailing slashes
212-
*/
213-
214-
this.options = function(opts) {
215-
extend(options, opts);
216-
return this;
217-
}
218-
219-
220-
221-
222219
this.$get = ['$rootScope', '$location', '$routeParams', '$q', '$injector', '$http', '$templateCache',
223220
function( $rootScope, $location, $routeParams, $q, $injector, $http, $templateCache) {
224221

@@ -464,20 +461,14 @@ function $RouteProvider(){
464461
});
465462

466463
var ret = {};
464+
ret.base = basePath(scope.$parent);
467465
ret.when = bind(ret, when, scope);
468466
ret.otherwise = bind(ret, otherwise, scope);
469467
ret.updateRoute = bind(null, updateRoute, scope);
470468
return ret;
471469
},
472470

473-
scoped: function(scope) {
474-
var key = scopeKey(scope);
475-
if (key in scopedRoutes)
476-
return scopedRoutes[key];
477-
else {
478-
return $route.scoped(scope.$parent);
479-
}
480-
}
471+
scoped: scoped
481472
};
482473

483474
$route.__defineGetter__('current', function() {
@@ -491,6 +482,12 @@ function $RouteProvider(){
491482
return $route;
492483

493484
/////////////////////////////////////////////////////
485+
486+
function basePath(scope) {
487+
var current = $route.scoped(scope).current;
488+
if (!current) return;
489+
return current.base;
490+
}
494491

495492
// http://github.com/visionmedia/express
496493
function switchRouteMatcher(on, route) {
@@ -499,9 +496,14 @@ function $RouteProvider(){
499496

500497
if (!route.regexp) return null;
501498

499+
500+
502501
var m = route.regexp.exec(on);
503502
if (!m) return null;
504503

504+
if (route.ellipsis)
505+
route.base = m[0];
506+
505507
var N = 0;
506508
for (var i = 1, len = m.length; i < len; ++i) {
507509
var key = keys[i - 1];
@@ -526,8 +528,9 @@ function $RouteProvider(){
526528
last = route.current;
527529

528530
if (next && last && next.$route === last.$route &&
529-
(!next.reloadOnParams || equals(next.pathParams, last.pathParams) && !next.reloadOnSearch) &&
530-
!forceReload) {
531+
equals(next.pathParams, last.pathParams) &&
532+
(!(!equals(next.params, last.params) && next.reloadOnSearch)) &&
533+
!forceReload) {
531534
last.params = next.params;
532535
if (!scope)
533536
copy(last.params, $routeParams);
@@ -605,6 +608,7 @@ function $RouteProvider(){
605608
// Match a route
606609
var routes = $route.scoped(scope).routes,
607610
params, match;
611+
608612
forEach(routes, function(route, path) {
609613
if (!match && (params = matcher($location.path(), route))) {
610614
match = inherit(route, {

test/ng/directive/ngViewSpec.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ describe('ngView', function() {
469469

470470
function RootViewCtrl($scope) {
471471
$scope.$router = function(router) {
472-
router.when('/foo/:bar', {templateUrl: 'childTpl.html', controller: ChildViewCtrl});
472+
router.when(':bar', {templateUrl: 'childTpl.html', controller: ChildViewCtrl});
473473
}
474474
}
475475

@@ -478,7 +478,7 @@ describe('ngView', function() {
478478
}
479479

480480
module(function($routeProvider) {
481-
$routeProvider.when('/foo/*', {templateUrl: 'tpl.html', controller: RootViewCtrl});
481+
$routeProvider.when('/foo/...', {templateUrl: 'tpl.html', controller: RootViewCtrl});
482482
});
483483

484484
inject(function($templateCache, $location, $rootScope, $route) {
@@ -497,8 +497,8 @@ describe('ngView', function() {
497497

498498
function RootViewCtrl($scope) {
499499
$scope.$router = function(router) {
500-
router.when('/foo/tpl1', {templateUrl: 'childTpl.html', controller: ChildViewCtrl});
501-
router.when('/foo/tpl2', {templateUrl: 'childTpl2.html', controller: ChildViewCtrl});
500+
router.when('tpl1', {templateUrl: 'childTpl.html', controller: ChildViewCtrl});
501+
router.when('tpl2', {templateUrl: 'childTpl2.html', controller: ChildViewCtrl});
502502
}
503503
}
504504

@@ -507,10 +507,9 @@ describe('ngView', function() {
507507
}
508508

509509
module(function($routeProvider) {
510-
$routeProvider.when('/foo/*', {
510+
$routeProvider.when('/foo/...', {
511511
templateUrl: 'tpl.html',
512-
controller: RootViewCtrl,
513-
reloadOnParams: false}
512+
controller: RootViewCtrl}
514513
);
515514
});
516515

test/ng/routeParamsSpec.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,6 @@ describe('$routeParams', function() {
8686
$rootScope.$digest();
8787
expect($routeParams).toEqual({0: '12'});
8888
});
89-
<<<<<<< HEAD
90-
=======
9189
});
9290

9391
it('should correctly ignore params in ellipsees', function() {
@@ -99,10 +97,9 @@ describe('$routeParams', function() {
9997
$location.path('/bar/foovalue');
10098
$rootScope.$digest();
10199
expect($routeParams).toEqual({});
100+
expect($route.current).toBeDefined()
102101

103102
});
104-
>>>>>>> express-style-route-matching
105-
106103
});
107104

108105

test/ng/routeSpec.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -739,29 +739,27 @@ describe('$route', function() {
739739
it('should override parent route', function() {
740740

741741
module(function($routeProvider) {
742-
$routeProvider.when('/foo/*');
742+
$routeProvider.when('/foo/...');
743743
});
744744

745-
746-
747745
inject(function($route, $location, $rootScope, $routeParams) {
748746
$location.path('/foo/barvalue');
749747
$rootScope.$digest();
750-
expect($routeParams).toEqual({0: 'barvalue'});
748+
expect($routeParams).toEqual({});
751749

752750
var childScope = $rootScope.$new();
753751
var router = $route.scopedRouter(childScope);
754-
router.when('/foo/:bar');
752+
router.when(':bar');
755753
$route.reload();
756754
$rootScope.$digest();
757-
expect($routeParams).toEqual({0: 'barvalue'});
758-
expect($rootScope.$routeParams).toEqual({0: 'barvalue'});
755+
expect($routeParams).toEqual({});
756+
expect($rootScope.$routeParams).toEqual({});
759757
expect(childScope.$routeParams).toEqual({bar: 'barvalue'});
760758

761759
childScope.$destroy();
762760
$route.reload();
763761
$rootScope.$digest();
764-
expect($routeParams).toEqual({0: 'barvalue'});
762+
expect($routeParams).toEqual({});
765763

766764
});
767765

0 commit comments

Comments
 (0)