Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit f16bd2f

Browse files
mheveryvojtajina
authored andcommitted
refactor($route): move when/otherwise to provider
1 parent ef7346f commit f16bd2f

File tree

4 files changed

+592
-498
lines changed

4 files changed

+592
-498
lines changed

src/service/route.js

+73-72
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,78 @@
6363
</doc:example>
6464
*/
6565
function $RouteProvider(){
66+
var routes = {};
67+
68+
/**
69+
* @ngdoc method
70+
* @name angular.module.ng.$route#when
71+
* @methodOf angular.module.ng.$route
72+
*
73+
* @param {string} path Route path (matched against `$location.hash`)
74+
* @param {Object} route Mapping information to be assigned to `$route.current` on route
75+
* match.
76+
*
77+
* Object properties:
78+
*
79+
* - `controller` – `{function()=}` – Controller fn that should be associated with newly
80+
* created scope.
81+
* - `template` – `{string=}` – path to an html template that should be used by
82+
* {@link angular.module.ng.$compileProvider.directive.ng:view ng:view} or
83+
* {@link angular.module.ng.$compileProvider.directive.ng:include ng:include} widgets.
84+
* - `redirectTo` – {(string|function())=} – value to update
85+
* {@link angular.module.ng.$location $location} path with and trigger route redirection.
86+
*
87+
* If `redirectTo` is a function, it will be called with the following parameters:
88+
*
89+
* - `{Object.<string>}` - route parameters extracted from the current
90+
* `$location.path()` by applying the current route template.
91+
* - `{string}` - current `$location.path()`
92+
* - `{Object}` - current `$location.search()`
93+
*
94+
* The custom `redirectTo` function is expected to return a string which will be used
95+
* to update `$location.path()` and `$location.search()`.
96+
*
97+
* - `[reloadOnSearch=true]` - {boolean=} - reload route when only $location.search()
98+
* changes.
99+
*
100+
* If the option is set to false and url in the browser changes, then
101+
* $routeUpdate event is emited on the current route scope. You can use this event to
102+
* react to {@link angular.module.ng.$routeParams} changes:
103+
*
104+
* function MyCtrl($route, $routeParams) {
105+
* this.$on('$routeUpdate', function() {
106+
* // do stuff with $routeParams
107+
* });
108+
* }
109+
*
110+
* @returns {Object} route object
111+
*
112+
* @description
113+
* Adds a new route definition to the `$route` service.
114+
*/
115+
this.when = function(path, route) {
116+
var routeDef = routes[path];
117+
if (!routeDef) routeDef = routes[path] = {reloadOnSearch: true};
118+
if (route) extend(routeDef, route); // TODO(im): what the heck? merge two route definitions?
119+
return routeDef;
120+
};
121+
122+
/**
123+
* @ngdoc method
124+
* @name angular.module.ng.$route#otherwise
125+
* @methodOf angular.module.ng.$route
126+
*
127+
* @description
128+
* Sets route definition that will be used on route change when no other route definition
129+
* is matched.
130+
*
131+
* @param {Object} params Mapping information to be assigned to `$route.current`.
132+
*/
133+
this.otherwise = function(params) {
134+
this.when(null, params);
135+
};
136+
137+
66138
this.$get = ['$rootScope', '$location', '$routeParams', '$controller',
67139
function( $rootScope, $location, $routeParams, $controller) {
68140
/**
@@ -112,8 +184,7 @@ function $RouteProvider(){
112184
* instance of the Controller.
113185
*/
114186

115-
var routes = {},
116-
matcher = switchRouteMatcher,
187+
var matcher = switchRouteMatcher,
117188
parentScope = $rootScope,
118189
dirty = 0,
119190
forceReload = false,
@@ -136,76 +207,6 @@ function $RouteProvider(){
136207
if (scope) parentScope = scope;
137208
},
138209

139-
/**
140-
* @ngdoc method
141-
* @name angular.module.ng.$route#when
142-
* @methodOf angular.module.ng.$route
143-
*
144-
* @param {string} path Route path (matched against `$location.hash`)
145-
* @param {Object} route Mapping information to be assigned to `$route.current` on route
146-
* match.
147-
*
148-
* Object properties:
149-
*
150-
* - `controller` – `{function()=}` – Controller fn that should be associated with newly
151-
* created scope.
152-
* - `template` – `{string=}` – path to an html template that should be used by
153-
* {@link angular.module.ng.$compileProvider.directive.ng:view ng:view} or
154-
* {@link angular.module.ng.$compileProvider.directive.ng:include ng:include} widgets.
155-
* - `redirectTo` – {(string|function())=} – value to update
156-
* {@link angular.module.ng.$location $location} path with and trigger route redirection.
157-
*
158-
* If `redirectTo` is a function, it will be called with the following parameters:
159-
*
160-
* - `{Object.<string>}` - route parameters extracted from the current
161-
* `$location.path()` by applying the current route template.
162-
* - `{string}` - current `$location.path()`
163-
* - `{Object}` - current `$location.search()`
164-
*
165-
* The custom `redirectTo` function is expected to return a string which will be used
166-
* to update `$location.path()` and `$location.search()`.
167-
*
168-
* - `[reloadOnSearch=true]` - {boolean=} - reload route when only $location.search()
169-
* changes.
170-
*
171-
* If the option is set to false and url in the browser changes, then
172-
* $routeUpdate event is emited on the current route scope. You can use this event to
173-
* react to {@link angular.module.ng.$routeParams} changes:
174-
*
175-
* function MyCtrl($route, $routeParams) {
176-
* this.$on('$routeUpdate', function() {
177-
* // do stuff with $routeParams
178-
* });
179-
* }
180-
*
181-
* @returns {Object} route object
182-
*
183-
* @description
184-
* Adds a new route definition to the `$route` service.
185-
*/
186-
when: function(path, route) {
187-
var routeDef = routes[path];
188-
if (!routeDef) routeDef = routes[path] = {reloadOnSearch: true};
189-
if (route) extend(routeDef, route); // TODO(im): what the heck? merge two route definitions?
190-
dirty++;
191-
return routeDef;
192-
},
193-
194-
/**
195-
* @ngdoc method
196-
* @name angular.module.ng.$route#otherwise
197-
* @methodOf angular.module.ng.$route
198-
*
199-
* @description
200-
* Sets route definition that will be used on route change when no other route definition
201-
* is matched.
202-
*
203-
* @param {Object} params Mapping information to be assigned to `$route.current`.
204-
*/
205-
otherwise: function(params) {
206-
$route.when(null, params);
207-
},
208-
209210
/**
210211
* @ngdoc method
211212
* @name angular.module.ng.$route#reload

test/service/routeParamsSpec.js

+14-10
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
'use strict';
22

33
describe('$routeParams', function() {
4-
it('should publish the params into a service', inject(function($rootScope, $route, $location, $routeParams) {
5-
$route.when('/foo');
6-
$route.when('/bar/:barId');
4+
it('should publish the params into a service', function() {
5+
module(function($routeProvider) {
6+
$routeProvider.when('/foo');
7+
$routeProvider.when('/bar/:barId');
8+
});
79

8-
$location.path('/foo').search('a=b');
9-
$rootScope.$digest();
10-
expect($routeParams).toEqual({a:'b'});
10+
inject(function($rootScope, $route, $location, $routeParams) {
11+
$location.path('/foo').search('a=b');
12+
$rootScope.$digest();
13+
expect($routeParams).toEqual({a:'b'});
1114

12-
$location.path('/bar/123').search('x=abc');
13-
$rootScope.$digest();
14-
expect($routeParams).toEqual({barId:'123', x:'abc'});
15-
}));
15+
$location.path('/bar/123').search('x=abc');
16+
$rootScope.$digest();
17+
expect($routeParams).toEqual({barId:'123', x:'abc'});
18+
});
19+
});
1620
});

0 commit comments

Comments
 (0)