Skip to content

Commit 6b2a1b2

Browse files
committed
Add handleCancelledTransition hook
1 parent 748c7ff commit 6b2a1b2

File tree

1 file changed

+34
-18
lines changed

1 file changed

+34
-18
lines changed

modules/components/Route.js

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,30 @@ var Route = React.createClass({
7474

7575
statics: {
7676

77-
handleAsyncError: function (error) {
77+
getUnreservedProps: function (props) {
78+
return withoutProperties(props, RESERVED_PROPS);
79+
},
80+
81+
/**
82+
* Handles errors that were thrown asynchronously. By default, the
83+
* error is re-thrown so we don't swallow them silently.
84+
*/
85+
handleAsyncError: function (error, route) {
7886
throw error; // This error probably originated in a transition hook.
7987
},
8088

81-
getUnreservedProps: function (props) {
82-
return withoutProperties(props, RESERVED_PROPS);
89+
/**
90+
* Handles cancelled transitions. By default, redirects replace the
91+
* current URL and aborts roll it back.
92+
*/
93+
handleCancelledTransition: function (transition, route) {
94+
var reason = transition.cancelReason;
95+
96+
if (reason instanceof Redirect) {
97+
replaceWith(reason.to, reason.params, reason.query);
98+
} else if (reason instanceof Abort) {
99+
goBack();
100+
}
83101
}
84102

85103
},
@@ -169,30 +187,28 @@ var Route = React.createClass({
169187
*/
170188
dispatch: function (path, returnRejectedPromise) {
171189
var transition = new Transition(path);
190+
var route = this;
172191

173-
return syncWithTransition(this, transition).then(function (newState) {
192+
var promise = syncWithTransition(route, transition).then(function (newState) {
174193
if (transition.isCancelled) {
175-
var reason = transition.cancelReason;
176-
177-
if (reason instanceof Redirect) {
178-
replaceWith(reason.to, reason.params, reason.query);
179-
} else if (reason instanceof Abort) {
180-
goBack();
181-
}
194+
Route.handleCancelledTransition(transition, route);
182195
} else if (newState) {
183196
ActiveStore.update(newState);
184197
}
185198

186199
return transition;
187-
}).then(undefined, function (error) {
188-
if (returnRejectedPromise)
189-
throw error;
200+
});
190201

191-
// Use setTimeout to break the promise chain.
192-
setTimeout(function () {
193-
Route.handleAsyncError(error);
202+
if (!returnRejectedPromise) {
203+
promise = promise.then(undefined, function (error) {
204+
// Use setTimeout to break the promise chain.
205+
setTimeout(function () {
206+
Route.handleAsyncError(error, route);
207+
});
194208
});
195-
});
209+
}
210+
211+
return promise;
196212
},
197213

198214
render: function () {

0 commit comments

Comments
 (0)