From 58073ca1d5af16a5fc3e7471f9c95fb87209e4e6 Mon Sep 17 00:00:00 2001 From: Michael Jackson Date: Thu, 14 Aug 2014 11:29:01 -0700 Subject: [PATCH] [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. --- modules/components/Routes.js | 59 +++++++++-------------------------- modules/helpers/Redirect.js | 10 ++++++ modules/helpers/Transition.js | 34 ++++++++++++++++++++ 3 files changed, 58 insertions(+), 45 deletions(-) create mode 100644 modules/helpers/Redirect.js create mode 100644 modules/helpers/Transition.js diff --git a/modules/components/Routes.js b/modules/components/Routes.js index 0c48a510d6..e6eff8e6b3 100644 --- a/modules/components/Routes.js +++ b/modules/components/Routes.js @@ -2,11 +2,12 @@ var React = require('react'); var warning = require('react/lib/warning'); var copyProperties = require('react/lib/copyProperties'); var Promise = require('es6-promise').Promise; +var Route = require('../components/Route'); var goBack = require('../helpers/goBack'); var replaceWith = require('../helpers/replaceWith'); -var transitionTo = require('../helpers/transitionTo'); -var Route = require('../components/Route'); var Path = require('../helpers/Path'); +var Redirect = require('../helpers/Redirect'); +var Transition = require('../helpers/Transition'); var HashLocation = require('../locations/HashLocation'); var HistoryLocation = require('../locations/HistoryLocation'); var RefreshLocation = require('../locations/RefreshLocation'); @@ -49,15 +50,15 @@ var Routes = React.createClass({ }, /** - * Handles cancelled transitions. By default, redirects replace the - * current URL and aborts roll it back. + * Handles aborted transitions. By default, redirects replace the + * current URL and all others roll it back. */ - handleCancelledTransition: function (transition, routes) { - var reason = transition.cancelReason; + handleAbortedTransition: function (transition, routes) { + var reason = transition.abortReason; if (reason instanceof Redirect) { replaceWith(reason.to, reason.params, reason.query); - } else if (reason instanceof Abort) { + } else { goBack(); } } @@ -174,8 +175,8 @@ var Routes = React.createClass({ var routes = this; var promise = syncWithTransition(routes, transition).then(function (newState) { - if (transition.isCancelled) { - Routes.handleCancelledTransition(transition, routes); + if (transition.isAborted) { + Routes.handleAbortedTransition(transition, routes); } else if (newState) { ActiveStore.updateState(newState); } @@ -210,38 +211,6 @@ var Routes = React.createClass({ }); -function Transition(path) { - this.path = path; - this.cancelReason = null; - this.isCancelled = false; -} - -copyProperties(Transition.prototype, { - - abort: function () { - this.cancelReason = new Abort(); - this.isCancelled = true; - }, - - redirect: function (to, params, query) { - this.cancelReason = new Redirect(to, params, query); - this.isCancelled = true; - }, - - retry: function () { - transitionTo(this.path); - } - -}); - -function Abort() {} - -function Redirect(to, params, query) { - this.to = to; - this.params = params; - this.query = query; -} - function findMatches(path,route){ var matches = null; @@ -360,11 +329,11 @@ function syncWithTransition(routes, transition) { } return checkTransitionFromHooks(fromMatches, transition).then(function () { - if (transition.isCancelled) + if (transition.isAborted) return; // No need to continue. return checkTransitionToHooks(toMatches, transition).then(function () { - if (transition.isCancelled) + if (transition.isAborted) return; // No need to continue. var rootMatch = getRootMatch(nextMatches); @@ -402,7 +371,7 @@ function checkTransitionFromHooks(matches, transition) { promise = promise.then(function () { var handler = match.route.props.handler; - if (!transition.isCancelled && handler.willTransitionFrom) + if (!transition.isAborted && handler.willTransitionFrom) return handler.willTransitionFrom(transition, match.component); }); }); @@ -422,7 +391,7 @@ function checkTransitionToHooks(matches, transition) { promise = promise.then(function () { var handler = match.route.props.handler; - if (!transition.isCancelled && handler.willTransitionTo) + if (!transition.isAborted && handler.willTransitionTo) return handler.willTransitionTo(transition, match.params); }); }); diff --git a/modules/helpers/Redirect.js b/modules/helpers/Redirect.js new file mode 100644 index 0000000000..0b92449b82 --- /dev/null +++ b/modules/helpers/Redirect.js @@ -0,0 +1,10 @@ +/** + * Encapsulates a redirect to the given route. + */ +function Redirect(to, params, query) { + this.to = to; + this.params = params; + this.query = query; +} + +module.exports = Redirect; diff --git a/modules/helpers/Transition.js b/modules/helpers/Transition.js new file mode 100644 index 0000000000..4404e21583 --- /dev/null +++ b/modules/helpers/Transition.js @@ -0,0 +1,34 @@ +var copyProperties = require('react/lib/copyProperties'); +var transitionTo = require('./transitionTo'); +var Redirect = require('./Redirect'); + +/** + * Encapsulates a transition to a given path. + * + * The willTransitionTo and willTransitionFrom handlers receive + * an instance of this class as their first argument. + */ +function Transition(path) { + this.path = path; + this.abortReason = null; + this.isAborted = false; +} + +copyProperties(Transition.prototype, { + + abort: function (reason) { + this.abortReason = reason; + this.isAborted = true; + }, + + redirect: function (to, params, query) { + this.abort(new Redirect(to, params, query)); + }, + + retry: function () { + transitionTo(this.path); + } + +}); + +module.exports = Transition;