Skip to content

Commit

Permalink
[added] Transition#cancel
Browse files Browse the repository at this point in the history
[changed] Transition#to/from => Transition.to/from
  • Loading branch information
mjackson committed Feb 13, 2015
1 parent b06e34f commit e05e229
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 60 deletions.
107 changes: 50 additions & 57 deletions modules/Transition.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* jshint -W058 */
var assign = require('react/lib/Object.assign');

var Cancellation = require('./Cancellation');
var Redirect = require('./Redirect');

/**
Expand All @@ -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;
6 changes: 3 additions & 3 deletions modules/createRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ function createRouter(options) {

cancelPendingTransition: function () {
if (pendingTransition) {
pendingTransition.abort(new Cancellation);
pendingTransition.cancel();
pendingTransition = null;
}
},
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit e05e229

Please sign in to comment.