Skip to content

Commit 4143b2c

Browse files
committed
Fix redirects regression and update auth-flow example
1 parent bd413ff commit 4143b2c

File tree

4 files changed

+123
-75
lines changed

4 files changed

+123
-75
lines changed

examples/auth-flow/app.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ var Dashboard = React.createClass({
6767
});
6868

6969
var Login = React.createClass({
70+
mixins: [ Router.Navigation ],
71+
7072
statics: {
7173
attemptedTransition: null
7274
},
@@ -90,7 +92,7 @@ var Login = React.createClass({
9092
Login.attemptedTransition = null;
9193
transition.retry();
9294
} else {
93-
Router.replaceWith('/about');
95+
this.replaceWith('/about');
9496
}
9597
}.bind(this));
9698
},

modules/components/Routes.js

+107-20
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
var React = require('react');
22
var warning = require('react/lib/warning');
3+
var invariant = require('react/lib/invariant');
34
var canUseDOM = require('react/lib/ExecutionEnvironment').canUseDOM;
45
var copyProperties = require('react/lib/copyProperties');
56
var PathStore = require('../stores/PathStore');
7+
var HashLocation = require('../locations/HashLocation');
68
var reversedArray = require('../utils/reversedArray');
79
var Transition = require('../utils/Transition');
810
var Redirect = require('../utils/Redirect');
@@ -262,36 +264,30 @@ function computeHandlerProps(matches, query) {
262264

263265
var BrowserTransitionHandling = {
264266

265-
handleTransitionError: function (error) {
267+
handleTransitionError: function (component, error) {
266268
throw error; // This error probably originated in a transition hook.
267269
},
268270

269-
handleAbortedTransition: function (transition) {
271+
handleAbortedTransition: function (component, transition) {
270272
var reason = transition.abortReason;
271273

272274
if (reason instanceof Redirect) {
273-
this.replaceWith(reason.to, reason.params, reason.query);
275+
component.replaceWith(reason.to, reason.params, reason.query);
274276
} else {
275-
this.goBack();
277+
component.goBack();
276278
}
277279
}
278280

279281
};
280282

281283
var ServerTransitionHandling = {
282284

283-
handleTransitionError: function (error) {
285+
handleTransitionError: function (component, error) {
284286
// TODO
285287
},
286288

287-
handleAbortedTransition: function (transition) {
288-
var reason = transition.abortReason;
289-
290-
if (reason instanceof Redirect) {
291-
// TODO
292-
} else {
293-
// TODO
294-
}
289+
handleAbortedTransition: function (component, transition) {
290+
// TODO
295291
}
296292

297293
};
@@ -351,9 +347,9 @@ var Routes = React.createClass({
351347

352348
this.dispatch(path, function (error, transition) {
353349
if (error) {
354-
TransitionHandling.handleTransitionError.call(self, error);
350+
TransitionHandling.handleTransitionError(self, error);
355351
} else if (transition.isAborted) {
356-
TransitionHandling.handleAbortedTransition.call(self, transition);
352+
TransitionHandling.handleAbortedTransition(self, transition);
357353
} else {
358354
self.updateScroll(path, actionType);
359355
}
@@ -417,6 +413,13 @@ var Routes = React.createClass({
417413
return computeHandlerProps(this.state.matches, this.state.activeQuery);
418414
},
419415

416+
/**
417+
* Returns a reference to the active route handler's component instance.
418+
*/
419+
getActiveComponent: function () {
420+
return this.refs.__activeRoute__;
421+
},
422+
420423
/**
421424
* Returns the current URL path.
422425
*/
@@ -425,10 +428,84 @@ var Routes = React.createClass({
425428
},
426429

427430
/**
428-
* Returns a reference to the active route handler's component instance.
431+
* Returns an absolute URL path created from the given route
432+
* name, URL parameters, and query values.
429433
*/
430-
getActiveComponent: function () {
431-
return this.refs.__activeRoute__;
434+
makePath: function (to, params, query) {
435+
var path;
436+
if (Path.isAbsolute(to)) {
437+
path = Path.normalize(to);
438+
} else {
439+
var namedRoutes = this.getNamedRoutes();
440+
var route = namedRoutes[to];
441+
442+
invariant(
443+
route,
444+
'Unable to find a route named "' + to + '". Make sure you have ' +
445+
'a <Route name="' + to + '"> defined somewhere in your <Routes>'
446+
);
447+
448+
path = route.props.path;
449+
}
450+
451+
return Path.withQuery(Path.injectParams(path, params), query);
452+
},
453+
454+
/**
455+
* Returns a string that may safely be used as the href of a
456+
* link to the route with the given name.
457+
*/
458+
makeHref: function (to, params, query) {
459+
var path = this.makePath(to, params, query);
460+
461+
if (this.getLocation() === HashLocation)
462+
return '#' + path;
463+
464+
return path;
465+
},
466+
467+
/**
468+
* Transitions to the URL specified in the arguments by pushing
469+
* a new URL onto the history stack.
470+
*/
471+
transitionTo: function (to, params, query) {
472+
var location = this.getLocation();
473+
474+
invariant(
475+
location,
476+
'You cannot use transitionTo without a location'
477+
);
478+
479+
location.push(this.makePath(to, params, query));
480+
},
481+
482+
/**
483+
* Transitions to the URL specified in the arguments by replacing
484+
* the current URL in the history stack.
485+
*/
486+
replaceWith: function (to, params, query) {
487+
var location = this.getLocation();
488+
489+
invariant(
490+
location,
491+
'You cannot use replaceWith without a location'
492+
);
493+
494+
location.replace(this.makePath(to, params, query));
495+
},
496+
497+
/**
498+
* Transitions to the previous URL.
499+
*/
500+
goBack: function () {
501+
var location = this.getLocation();
502+
503+
invariant(
504+
location,
505+
'You cannot use goBack without a location'
506+
);
507+
508+
location.pop();
432509
},
433510

434511
render: function () {
@@ -443,12 +520,22 @@ var Routes = React.createClass({
443520
},
444521

445522
childContextTypes: {
446-
currentPath: React.PropTypes.string
523+
currentPath: React.PropTypes.string,
524+
makePath: React.PropTypes.func.isRequired,
525+
makeHref: React.PropTypes.func.isRequired,
526+
transitionTo: React.PropTypes.func.isRequired,
527+
replaceWith: React.PropTypes.func.isRequired,
528+
goBack: React.PropTypes.func.isRequired
447529
},
448530

449531
getChildContext: function () {
450532
return {
451-
currentPath: this.getCurrentPath()
533+
currentPath: this.getCurrentPath(),
534+
makePath: this.makePath,
535+
makeHref: this.makeHref,
536+
transitionTo: this.transitionTo,
537+
replaceWith: this.replaceWith,
538+
goBack: this.goBack
452539
};
453540
}
454541

modules/mixins/Navigation.js

+10-51
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,55 @@
11
var React = require('react');
2-
var invariant = require('react/lib/invariant');
3-
var HashLocation = require('../locations/HashLocation');
4-
var Path = require('../utils/Path');
52

63
/**
74
* A mixin for components that modify the URL.
85
*/
96
var Navigation = {
107

118
contextTypes: {
12-
location: React.PropTypes.object, // Not required on the server.
13-
namedRoutes: React.PropTypes.object.isRequired
9+
makePath: React.PropTypes.func.isRequired,
10+
makeHref: React.PropTypes.func.isRequired,
11+
transitionTo: React.PropTypes.func.isRequired,
12+
replaceWith: React.PropTypes.func.isRequired,
13+
goBack: React.PropTypes.func.isRequired
1414
},
1515

1616
/**
1717
* Returns an absolute URL path created from the given route
1818
* name, URL parameters, and query values.
1919
*/
2020
makePath: function (to, params, query) {
21-
var path;
22-
if (Path.isAbsolute(to)) {
23-
path = Path.normalize(to);
24-
} else {
25-
var route = this.context.namedRoutes[to];
26-
27-
invariant(
28-
route,
29-
'Unable to find a route named "' + to + '". Make sure you have ' +
30-
'a <Route name="' + to + '"> defined somewhere in your <Routes>'
31-
);
32-
33-
path = route.props.path;
34-
}
35-
36-
return Path.withQuery(Path.injectParams(path, params), query);
21+
return this.context.makePath(to, params, query);
3722
},
3823

3924
/**
4025
* Returns a string that may safely be used as the href of a
4126
* link to the route with the given name.
4227
*/
4328
makeHref: function (to, params, query) {
44-
var path = this.makePath(to, params, query);
45-
46-
if (this.context.location === HashLocation)
47-
return '#' + path;
48-
49-
return path;
29+
return this.context.makeHref(to, params, query);
5030
},
5131

5232
/**
5333
* Transitions to the URL specified in the arguments by pushing
5434
* a new URL onto the history stack.
5535
*/
5636
transitionTo: function (to, params, query) {
57-
var location = this.context.location;
58-
59-
invariant(
60-
location,
61-
'You cannot use transitionTo without a location'
62-
);
63-
64-
location.push(this.makePath(to, params, query));
37+
this.context.transitionTo(to, params, query);
6538
},
6639

6740
/**
6841
* Transitions to the URL specified in the arguments by replacing
6942
* the current URL in the history stack.
7043
*/
7144
replaceWith: function (to, params, query) {
72-
var location = this.context.location;
73-
74-
invariant(
75-
location,
76-
'You cannot use replaceWith without a location'
77-
);
78-
79-
location.replace(this.makePath(to, params, query));
45+
this.context.replaceWith(to, params, query);
8046
},
8147

8248
/**
8349
* Transitions to the previous URL.
8450
*/
8551
goBack: function () {
86-
var location = this.context.location;
87-
88-
invariant(
89-
location,
90-
'You cannot use goBack without a location'
91-
);
92-
93-
location.pop();
52+
this.context.goBack();
9453
}
9554

9655
};

modules/utils/Transition.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ var Redirect = require('./Redirect');
88
* The willTransitionTo and willTransitionFrom handlers receive
99
* an instance of this class as their first argument.
1010
*/
11-
function Transition(pathDelegate, path) {
12-
this.pathDelegate = pathDelegate;
11+
function Transition(routesComponent, path) {
12+
this.routesComponent = routesComponent;
1313
this.path = path;
1414
this.abortReason = null;
1515
this.isAborted = false;
@@ -31,7 +31,7 @@ mixInto(Transition, {
3131
},
3232

3333
retry: function () {
34-
this.pathDelegate.replaceWith(this.path);
34+
this.routesComponent.replaceWith(this.path);
3535
}
3636

3737
});

0 commit comments

Comments
 (0)