Skip to content

Commit 3ff82dc

Browse files
authored
Merge pull request #5 from hjylewis/master
[redirectTo] new arguments
2 parents cd1d66f + de7f971 commit 3ff82dc

File tree

6 files changed

+96
-67
lines changed

6 files changed

+96
-67
lines changed

README.md

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -51,126 +51,138 @@ var app = angular.module('app', [
5151
*/
5252
app.config(['$middlewareProvider',
5353
function($middlewareProvider)] {
54-
54+
5555
// If you want middleware,
5656
// then you need to map some middleware
5757
// functions to names that you can
5858
// reference in your routes
5959
$middlewareProvider.map({
60-
60+
6161
/** Don't allow anyone through */
6262
'nobody': function nobodyMiddleware() {
6363
//
6464
},
65-
65+
6666
/** Let everyone through */
6767
'everyone': function everyoneMiddleware() {
6868
// In order to resolve the middleware,
6969
// you MUST call this.next()
7070
this.next();
7171
},
72-
72+
7373
/** Redirect everyone */
7474
'redirect-all': function redirectAllMiddleware() {
7575
// If you are using ui.router,
7676
// then you must choose a state name
7777
this.redirectTo('another-state-name');
78-
78+
7979
// If you are using ngRoute,
8080
// then you must actually put in
8181
// the new url that you would use in
8282
// $location.path()
8383
this.redirectTo('/another-path');
84+
85+
// An object of parameters can also
86+
// be provided which will be used to
87+
// populate the url query parameters
88+
// ex. /another-path?redirectFrom=current-path
89+
this.redirectTo('/another-path', {
90+
redirectFrom: 'current-path'
91+
});
92+
93+
// If you are using ui.router,
94+
// you can also change transitionTo options
95+
this.redirectTo('another-state-name', null, { reload: false });
8496
},
85-
97+
8698
/** Continue, but log the parameters */
8799
'log': ['$log', function logMiddleware($log) {
88100
// Notice that we used dependency injection to get $log.
89101
// You have access to the route parameters with this.params
90102
$log.debug(this.params);
91-
103+
92104
// Keep on truckin'
93105
this.next();
94106
}],
95-
107+
96108
/** It will wait for async requests too! */
97109
'async-auth': ['$http', function asyncAuth($http) {
98110
// We'll need access to "this" in a deeper context
99111
var request = this;
100-
112+
101113
// Grab something from the server
102114
$http.get('/verify')
103-
115+
104116
// The server has responded!
105117
.then(function success(res) {
106118
if ( res.isVerified ) {
107119
return request.next();
108120
}
109-
121+
110122
request.redirectTo('another-state-or-path');
111123
},
112-
124+
113125
function fail(err) {
114126
request.redirectTo('another-state-or-path');
115127
});
116128
}]
117-
129+
118130
});
119-
131+
120132
});
121133

122134
/**
123135
* Now you're ready to use your middleware!
124136
* All you have to do is put them in your routes.
125137
* Each middleware is processed in the order you list them.
126-
*
138+
*
127139
* The principle is the same for ui.router and ngRoute.
128140
* I'll show you both to make sure the dead horse is sufficiently beaten.
129141
*/
130-
142+
131143
/** ui.router */
132144
app.config(['$stateProvider', function($stateProvider) {
133145
$stateProvider
134-
146+
135147
// You can have just one middleware,
136148
// represented by a string
137149
.state('my-state-name', {
138150
...
139151
middleware: 'a-single-middleware'
140152
})
141-
153+
142154
// You can have multiple middleware
143155
// separated by pipes. aka. |
144156
.state('another-state-name', {
145157
...
146158
middleware: 'one-middleware|another-middleware'
147159
})
148-
160+
149161
// You can have multiple middleware as an array
150162
.state('a-third-state-name', {
151163
...
152164
middleware: ['one-middleware', 'another-middleware', 'another-nother-middleware']
153165
})
154166
}]);
155-
167+
156168
/** ngRoute */
157169
app.config(['$routeProvider', function($routeProvider) {
158170
$routeProvider
159-
171+
160172
// You can have just one middleware,
161173
// represented by a string
162174
.when('/my-path', {
163175
...
164176
middleware: 'a-single-middleware'
165177
})
166-
178+
167179
// You can have multiple middleware
168180
// separated by pipes. aka. |
169181
.when('/my-other-path', {
170182
...
171183
middleware: 'one-middleware|another-middleware'
172184
})
173-
185+
174186
// You can have multiple middleware as an array
175187
.when('/my-third-path', {
176188
...
@@ -195,6 +207,11 @@ function($middlewareProvider)] {
195207
196208
* `this.next()` **must be called** to resolve the middleware and either go to the next middleware or resolve the route
197209

198-
* `this.redirectTo(<string>)` can be called to immediately redirect to a given path _(ngRoute)_ or state name _(ui.router)_
210+
* `this.redirectTo(dest [,params [,options]])` can be called to immediately redirect
211+
* **dest** (required): A path _(ngRoute)_ or state name _(ui.router)_ to redirect to
212+
* **params** (optional): A params object to be used to populate query parameters _(ngRoute)_ or `$stateParams` _(ui.router)_
213+
* **options** (optional): An object of [transitionTo](https://github.com/angular-ui/ui-router/wiki/Quick-Reference#statetransitiontoto-toparams--options) options (only used with ui.router)
214+
215+
* `this.route` is the destination route path
199216

200217
* `this.params` is an object that contains the current route parameters

dist/angular-middleware.js

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ var _globalMiddleware = {
1313
var $middlewareFactory = [
1414
'$injector', '$q',
1515
function middlewareFactory($injector, $q) {
16-
16+
1717
/**
1818
* This object is used to group
1919
* private middleware properties
@@ -49,6 +49,9 @@ function middlewareFactory($injector, $q) {
4949
// Store a copy of the route parameters in the request
5050
request.params = angular.copy(toParams);
5151

52+
// Store route name in the request
53+
request.route = toRoute.name;
54+
5255
// Set the middleware index to 0
5356
middleware.index = 0;
5457

@@ -108,7 +111,7 @@ function middlewareFactory($injector, $q) {
108111

109112
/**
110113
* Gets the route middleware property
111-
*
114+
*
112115
* @param {object} route
113116
* @returns {array|string}
114117
*/
@@ -197,8 +200,13 @@ function middlewareFactory($injector, $q) {
197200
*
198201
* @returns {void}
199202
*/
200-
function redirectTo(route) {
201-
middleware.resolution.reject('redirectTo:' + route);
203+
function redirectTo(route, params, options) {
204+
middleware.resolution.reject({
205+
type: "redirectTo",
206+
route: route,
207+
params: params,
208+
options: options
209+
});
202210
}
203211
}];
204212

@@ -280,21 +288,19 @@ function handleMiddleware($rootScope, $route, $location, $middleware) {
280288
* Handle redirects from middleware
281289
*/
282290
$rootScope.$on('$routeChangeError', function handleMiddlewareRedirects(event, current, previous, rejection) {
283-
var pattern = /redirectTo\:(.*)/;
284-
var match;
285-
286-
// Only proceed if there is a match to the pattern
287-
if ((match = pattern.exec(rejection)) !== null) {
291+
// Only proceed if it is type redirectTo
292+
if (rejection.type === "redirectTo") {
288293
// Prevent the route change from working normally
289294
event.preventDefault();
290295

291296
// If the redirect route is the same, then just reload
292-
if ( current.regexp.test(match[1]) ) {
297+
if ( current.regexp.test(rejection.route) ) {
293298
return $route.reload();
294299
}
295300

296301
// The path is new, so go there!
297-
$location.path(match[1]);
302+
$location.path(rejection.route);
303+
if (rejection.params) $location.search(rejection.params);
298304
}
299305
});
300306
}]);
@@ -329,23 +335,23 @@ function handleMiddleware($rootScope, $state, $middleware) {
329335
* Handle redirects from middleware
330336
*/
331337
$rootScope.$on('$stateChangeError', function handleMiddlewareRedirects(event, toState, toParams, fromState, fromParams, error) {
332-
var pattern = /redirectTo\:(.*)/;
333-
var match;
334-
335-
// Only proceed if there is a match to the pattern
336-
if ((match = pattern.exec(error)) !== null) {
338+
// Only proceed if it is type redirectTo
339+
if (error.type === "redirectTo") {
337340
// Prevent state change error from working normally
338341
event.preventDefault();
339-
340-
// Redirect, allowing reloading and preventing url param inheritance
341-
// https://github.com/angular-ui/ui-router/wiki/Quick-Reference#statetransitiontoto-toparams--options
342-
return $state.transitionTo(match[1], null, {
342+
343+
// Use provided transitionTo options or use default
344+
var options = error.options || {
343345
location: true,
344346
inherit: false,
345347
relative: $state.$current,
346348
notify: true,
347349
reload: true
348-
});
350+
};
351+
352+
// Redirect, allowing reloading and preventing url param inheritance
353+
// https://github.com/angular-ui/ui-router/wiki/Quick-Reference#statetransitiontoto-toparams--options
354+
return $state.transitionTo(error.route, error.params, options);
349355
}
350356
});
351357
}]);

dist/angular-middleware.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/middleware.factory.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ var _globalMiddleware = {
1010
var $middlewareFactory = [
1111
'$injector', '$q',
1212
function middlewareFactory($injector, $q) {
13-
13+
1414
/**
1515
* This object is used to group
1616
* private middleware properties
@@ -46,6 +46,9 @@ function middlewareFactory($injector, $q) {
4646
// Store a copy of the route parameters in the request
4747
request.params = angular.copy(toParams);
4848

49+
// Store route name in the request
50+
request.route = toRoute.name;
51+
4952
// Set the middleware index to 0
5053
middleware.index = 0;
5154

@@ -105,7 +108,7 @@ function middlewareFactory($injector, $q) {
105108

106109
/**
107110
* Gets the route middleware property
108-
*
111+
*
109112
* @param {object} route
110113
* @returns {array|string}
111114
*/
@@ -194,7 +197,12 @@ function middlewareFactory($injector, $q) {
194197
*
195198
* @returns {void}
196199
*/
197-
function redirectTo(route) {
198-
middleware.resolution.reject('redirectTo:' + route);
200+
function redirectTo(route, params, options) {
201+
middleware.resolution.reject({
202+
type: "redirectTo",
203+
route: route,
204+
params: params,
205+
options: options
206+
});
199207
}
200208
}];

src/routers/ngRoute.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,19 @@ function handleMiddleware($rootScope, $route, $location, $middleware) {
2828
* Handle redirects from middleware
2929
*/
3030
$rootScope.$on('$routeChangeError', function handleMiddlewareRedirects(event, current, previous, rejection) {
31-
var pattern = /redirectTo\:(.*)/;
32-
var match;
33-
34-
// Only proceed if there is a match to the pattern
35-
if ((match = pattern.exec(rejection)) !== null) {
31+
// Only proceed if it is type redirectTo
32+
if (rejection.type === "redirectTo") {
3633
// Prevent the route change from working normally
3734
event.preventDefault();
3835

3936
// If the redirect route is the same, then just reload
40-
if ( current.regexp.test(match[1]) ) {
37+
if ( current.regexp.test(rejection.route) ) {
4138
return $route.reload();
4239
}
4340

4441
// The path is new, so go there!
45-
$location.path(match[1]);
42+
$location.path(rejection.route);
43+
if (rejection.params) $location.search(rejection.params);
4644
}
4745
});
4846
}]);

src/routers/uiRouter.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,23 @@ function handleMiddleware($rootScope, $state, $middleware) {
2828
* Handle redirects from middleware
2929
*/
3030
$rootScope.$on('$stateChangeError', function handleMiddlewareRedirects(event, toState, toParams, fromState, fromParams, error) {
31-
var pattern = /redirectTo\:(.*)/;
32-
var match;
33-
34-
// Only proceed if there is a match to the pattern
35-
if ((match = pattern.exec(error)) !== null) {
31+
// Only proceed if it is type redirectTo
32+
if (error.type === "redirectTo") {
3633
// Prevent state change error from working normally
3734
event.preventDefault();
38-
39-
// Redirect, allowing reloading and preventing url param inheritance
40-
// https://github.com/angular-ui/ui-router/wiki/Quick-Reference#statetransitiontoto-toparams--options
41-
return $state.transitionTo(match[1], null, {
35+
36+
// Use provided transitionTo options or use default
37+
var options = error.options || {
4238
location: true,
4339
inherit: false,
4440
relative: $state.$current,
4541
notify: true,
4642
reload: true
47-
});
43+
};
44+
45+
// Redirect, allowing reloading and preventing url param inheritance
46+
// https://github.com/angular-ui/ui-router/wiki/Quick-Reference#statetransitiontoto-toparams--options
47+
return $state.transitionTo(error.route, error.params, options);
4848
}
4949
});
5050
}]);

0 commit comments

Comments
 (0)