Skip to content

Commit 58073ca

Browse files
committed
[changed] Transition#cancelReason => abortReason
Also, transition.abort() now accepts a reason argument that indicates the reason for the abort. I anticipate that we'll need to add a NotFound reason soon.
1 parent 6d1ae95 commit 58073ca

File tree

3 files changed

+58
-45
lines changed

3 files changed

+58
-45
lines changed

modules/components/Routes.js

+14-45
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ var React = require('react');
22
var warning = require('react/lib/warning');
33
var copyProperties = require('react/lib/copyProperties');
44
var Promise = require('es6-promise').Promise;
5+
var Route = require('../components/Route');
56
var goBack = require('../helpers/goBack');
67
var replaceWith = require('../helpers/replaceWith');
7-
var transitionTo = require('../helpers/transitionTo');
8-
var Route = require('../components/Route');
98
var Path = require('../helpers/Path');
9+
var Redirect = require('../helpers/Redirect');
10+
var Transition = require('../helpers/Transition');
1011
var HashLocation = require('../locations/HashLocation');
1112
var HistoryLocation = require('../locations/HistoryLocation');
1213
var RefreshLocation = require('../locations/RefreshLocation');
@@ -49,15 +50,15 @@ var Routes = React.createClass({
4950
},
5051

5152
/**
52-
* Handles cancelled transitions. By default, redirects replace the
53-
* current URL and aborts roll it back.
53+
* Handles aborted transitions. By default, redirects replace the
54+
* current URL and all others roll it back.
5455
*/
55-
handleCancelledTransition: function (transition, routes) {
56-
var reason = transition.cancelReason;
56+
handleAbortedTransition: function (transition, routes) {
57+
var reason = transition.abortReason;
5758

5859
if (reason instanceof Redirect) {
5960
replaceWith(reason.to, reason.params, reason.query);
60-
} else if (reason instanceof Abort) {
61+
} else {
6162
goBack();
6263
}
6364
}
@@ -174,8 +175,8 @@ var Routes = React.createClass({
174175
var routes = this;
175176

176177
var promise = syncWithTransition(routes, transition).then(function (newState) {
177-
if (transition.isCancelled) {
178-
Routes.handleCancelledTransition(transition, routes);
178+
if (transition.isAborted) {
179+
Routes.handleAbortedTransition(transition, routes);
179180
} else if (newState) {
180181
ActiveStore.updateState(newState);
181182
}
@@ -210,38 +211,6 @@ var Routes = React.createClass({
210211

211212
});
212213

213-
function Transition(path) {
214-
this.path = path;
215-
this.cancelReason = null;
216-
this.isCancelled = false;
217-
}
218-
219-
copyProperties(Transition.prototype, {
220-
221-
abort: function () {
222-
this.cancelReason = new Abort();
223-
this.isCancelled = true;
224-
},
225-
226-
redirect: function (to, params, query) {
227-
this.cancelReason = new Redirect(to, params, query);
228-
this.isCancelled = true;
229-
},
230-
231-
retry: function () {
232-
transitionTo(this.path);
233-
}
234-
235-
});
236-
237-
function Abort() {}
238-
239-
function Redirect(to, params, query) {
240-
this.to = to;
241-
this.params = params;
242-
this.query = query;
243-
}
244-
245214
function findMatches(path,route){
246215
var matches = null;
247216

@@ -360,11 +329,11 @@ function syncWithTransition(routes, transition) {
360329
}
361330

362331
return checkTransitionFromHooks(fromMatches, transition).then(function () {
363-
if (transition.isCancelled)
332+
if (transition.isAborted)
364333
return; // No need to continue.
365334

366335
return checkTransitionToHooks(toMatches, transition).then(function () {
367-
if (transition.isCancelled)
336+
if (transition.isAborted)
368337
return; // No need to continue.
369338

370339
var rootMatch = getRootMatch(nextMatches);
@@ -402,7 +371,7 @@ function checkTransitionFromHooks(matches, transition) {
402371
promise = promise.then(function () {
403372
var handler = match.route.props.handler;
404373

405-
if (!transition.isCancelled && handler.willTransitionFrom)
374+
if (!transition.isAborted && handler.willTransitionFrom)
406375
return handler.willTransitionFrom(transition, match.component);
407376
});
408377
});
@@ -422,7 +391,7 @@ function checkTransitionToHooks(matches, transition) {
422391
promise = promise.then(function () {
423392
var handler = match.route.props.handler;
424393

425-
if (!transition.isCancelled && handler.willTransitionTo)
394+
if (!transition.isAborted && handler.willTransitionTo)
426395
return handler.willTransitionTo(transition, match.params);
427396
});
428397
});

modules/helpers/Redirect.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Encapsulates a redirect to the given route.
3+
*/
4+
function Redirect(to, params, query) {
5+
this.to = to;
6+
this.params = params;
7+
this.query = query;
8+
}
9+
10+
module.exports = Redirect;

modules/helpers/Transition.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
var copyProperties = require('react/lib/copyProperties');
2+
var transitionTo = require('./transitionTo');
3+
var Redirect = require('./Redirect');
4+
5+
/**
6+
* Encapsulates a transition to a given path.
7+
*
8+
* The willTransitionTo and willTransitionFrom handlers receive
9+
* an instance of this class as their first argument.
10+
*/
11+
function Transition(path) {
12+
this.path = path;
13+
this.abortReason = null;
14+
this.isAborted = false;
15+
}
16+
17+
copyProperties(Transition.prototype, {
18+
19+
abort: function (reason) {
20+
this.abortReason = reason;
21+
this.isAborted = true;
22+
},
23+
24+
redirect: function (to, params, query) {
25+
this.abort(new Redirect(to, params, query));
26+
},
27+
28+
retry: function () {
29+
transitionTo(this.path);
30+
}
31+
32+
});
33+
34+
module.exports = Transition;

0 commit comments

Comments
 (0)