Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b09ff84

Browse files
committed
allow redirect parameters
1 parent cd1d66f commit b09ff84

File tree

4 files changed

+36
-16
lines changed

4 files changed

+36
-16
lines changed

‎dist/angular-middleware.js‎

Lines changed: 18 additions & 8 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
@@ -108,7 +108,7 @@ function middlewareFactory($injector, $q) {
108108

109109
/**
110110
* Gets the route middleware property
111-
*
111+
*
112112
* @param {object} route
113113
* @returns {array|string}
114114
*/
@@ -197,8 +197,12 @@ function middlewareFactory($injector, $q) {
197197
*
198198
* @returns {void}
199199
*/
200-
function redirectTo(route) {
201-
middleware.resolution.reject('redirectTo:' + route);
200+
function redirectTo(route, params) {
201+
var redirectStr = 'redirectTo:' + route;
202+
if (params) {
203+
redirectStr = redirectStr + '(' + JSON.stringify(params) + ')';
204+
}
205+
middleware.resolution.reject(redirectStr);
202206
}
203207
}];
204208

@@ -280,7 +284,7 @@ function handleMiddleware($rootScope, $route, $location, $middleware) {
280284
* Handle redirects from middleware
281285
*/
282286
$rootScope.$on('$routeChangeError', function handleMiddlewareRedirects(event, current, previous, rejection) {
283-
var pattern = /redirectTo\:(.*)/;
287+
var pattern = /redirectTo\:([^\(]*)(\((\{.*\})\))?/;
284288
var match;
285289

286290
// Only proceed if there is a match to the pattern
@@ -295,6 +299,10 @@ function handleMiddleware($rootScope, $route, $location, $middleware) {
295299

296300
// The path is new, so go there!
297301
$location.path(match[1]);
302+
if (match[3]) {
303+
var params = JSON.parse(match[3]);
304+
$location.search(params);
305+
}
298306
}
299307
});
300308
}]);
@@ -329,17 +337,19 @@ function handleMiddleware($rootScope, $state, $middleware) {
329337
* Handle redirects from middleware
330338
*/
331339
$rootScope.$on('$stateChangeError', function handleMiddlewareRedirects(event, toState, toParams, fromState, fromParams, error) {
332-
var pattern = /redirectTo\:(.*)/;
340+
var pattern = /redirectTo\:([^\(]*)(\((\{.*\})\))?/;
333341
var match;
334342

335343
// Only proceed if there is a match to the pattern
336344
if ((match = pattern.exec(error)) !== null) {
337345
// Prevent state change error from working normally
338346
event.preventDefault();
339-
347+
348+
var params = match[3] ? JSON.parse(match[3]) : null;
349+
340350
// Redirect, allowing reloading and preventing url param inheritance
341351
// https://github.com/angular-ui/ui-router/wiki/Quick-Reference#statetransitiontoto-toparams--options
342-
return $state.transitionTo(match[1], null, {
352+
return $state.transitionTo(match[1], params, {
343353
location: true,
344354
inherit: false,
345355
relative: $state.$current,

‎src/middleware.factory.js‎

Lines changed: 8 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
@@ -105,7 +105,7 @@ function middlewareFactory($injector, $q) {
105105

106106
/**
107107
* Gets the route middleware property
108-
*
108+
*
109109
* @param {object} route
110110
* @returns {array|string}
111111
*/
@@ -194,7 +194,11 @@ function middlewareFactory($injector, $q) {
194194
*
195195
* @returns {void}
196196
*/
197-
function redirectTo(route) {
198-
middleware.resolution.reject('redirectTo:' + route);
197+
function redirectTo(route, params) {
198+
var redirectStr = 'redirectTo:' + route;
199+
if (params) {
200+
redirectStr = redirectStr + '(' + JSON.stringify(params) + ')';
201+
}
202+
middleware.resolution.reject(redirectStr);
199203
}
200204
}];

‎src/routers/ngRoute.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ 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\:(.*)/;
31+
var pattern = /redirectTo\:([^\(]*)(\((\{.*\})\))?/;
3232
var match;
3333

3434
// Only proceed if there is a match to the pattern
@@ -43,6 +43,10 @@ function handleMiddleware($rootScope, $route, $location, $middleware) {
4343

4444
// The path is new, so go there!
4545
$location.path(match[1]);
46+
if (match[3]) {
47+
var params = JSON.parse(match[3]);
48+
$location.search(params);
49+
}
4650
}
4751
});
4852
}]);

‎src/routers/uiRouter.js‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,19 @@ 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\:(.*)/;
31+
var pattern = /redirectTo\:([^\(]*)(\((\{.*\})\))?/;
3232
var match;
3333

3434
// Only proceed if there is a match to the pattern
3535
if ((match = pattern.exec(error)) !== null) {
3636
// Prevent state change error from working normally
3737
event.preventDefault();
38-
38+
39+
var params = match[3] ? JSON.parse(match[3]) : null;
40+
3941
// Redirect, allowing reloading and preventing url param inheritance
4042
// https://github.com/angular-ui/ui-router/wiki/Quick-Reference#statetransitiontoto-toparams--options
41-
return $state.transitionTo(match[1], null, {
43+
return $state.transitionTo(match[1], params, {
4244
location: true,
4345
inherit: false,
4446
relative: $state.$current,

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /