diff --git a/modules/Transition.js b/modules/Transition.js index e64923915a..2e01f459c1 100644 --- a/modules/Transition.js +++ b/modules/Transition.js @@ -1,5 +1,6 @@ /* jshint -W058 */ -var assign = require('react/lib/Object.assign'); + +var Cancellation = require('./Cancellation'); var Redirect = require('./Redirect'); /** @@ -14,69 +15,61 @@ function Transition(path, retry) { this.retry = retry.bind(this); } -assign(Transition.prototype, { - - abort: function (reason) { - if (this.abortReason == null) - this.abortReason = reason || 'ABORT'; - }, +Transition.prototype.abort = function (reason) { + if (this.abortReason == null) + this.abortReason = reason || 'ABORT'; +}; - redirect: function (to, params, query) { - this.abort(new Redirect(to, params, query)); - }, +Transition.prototype.redirect = function (to, params, query) { + this.abort(new Redirect(to, params, query)); +}; - from: function (routes, components, callback) { - var self = this; +Transition.prototype.cancel = function () { + this.abort(new Cancellation); +}; - var runHooks = routes.reduce(function (callback, route, index) { - return function (error) { - if (error || self.abortReason) { - callback(error); - } else if (route.willTransitionFrom) { - try { - route.willTransitionFrom(self, components[index], callback); +Transition.from = function (transition, routes, components, callback) { + routes.reduce(function (callback, route, index) { + return function (error) { + if (error || transition.abortReason) { + callback(error); + } else if (route.willTransitionFrom) { + try { + route.willTransitionFrom(transition, components[index], callback); - // If there is no callback in the argument list, call it automatically. - if (route.willTransitionFrom.length < 3) - callback(); - } catch (e) { - callback(e); - } - } else { - callback(); + // If there is no callback in the argument list, call it automatically. + if (route.willTransitionFrom.length < 3) + callback(); + } catch (e) { + callback(e); } - }; - }, callback); - - runHooks(); - }, + } else { + callback(); + } + }; + }, callback)(); +}; - to: function (routes, params, query, callback) { - var self = this; +Transition.to = function (transition, routes, params, query, callback) { + routes.reduceRight(function (callback, route) { + return function (error) { + if (error || transition.abortReason) { + callback(error); + } else if (route.willTransitionTo) { + try { + route.willTransitionTo(transition, params, query, callback); - var runHooks = routes.reduceRight(function (callback, route) { - return function (error) { - if (error || self.abortReason) { - callback(error); - } else if (route.willTransitionTo) { - try { - route.willTransitionTo(self, params, query, callback); - - // If there is no callback in the argument list, call it automatically. - if (route.willTransitionTo.length < 4) - callback(); - } catch (e) { - callback(e); - } - } else { - callback(); + // If there is no callback in the argument list, call it automatically. + if (route.willTransitionTo.length < 4) + callback(); + } catch (e) { + callback(e); } - }; - }, callback); - - runHooks(); - } - -}); + } else { + callback(); + } + }; + }, callback)(); +}; module.exports = Transition; diff --git a/modules/createRouter.js b/modules/createRouter.js index 5e3757f5c6..92778773d9 100644 --- a/modules/createRouter.js +++ b/modules/createRouter.js @@ -162,7 +162,7 @@ function createRouter(options) { cancelPendingTransition: function () { if (pendingTransition) { - pendingTransition.abort(new Cancellation); + pendingTransition.cancel(); pendingTransition = null; } }, @@ -385,11 +385,11 @@ function createRouter(options) { var fromComponents = mountedComponents.slice(prevRoutes.length - fromRoutes.length); - transition.from(fromRoutes, fromComponents, function (error) { + Transition.from(transition, fromRoutes, fromComponents, function (error) { if (error || transition.abortReason) return dispatchHandler.call(Router, error, transition); // No need to continue. - transition.to(toRoutes, nextParams, nextQuery, function (error) { + Transition.to(transition, toRoutes, nextParams, nextQuery, function (error) { dispatchHandler.call(Router, error, transition, { path: path, action: action,