Skip to content

Commit

Permalink
Use component in willTransitionFrom, not descriptor
Browse files Browse the repository at this point in the history
Fixes #47
  • Loading branch information
mjackson committed Jun 25, 2014
1 parent 63bdab2 commit a64d0cd
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions modules/components/Route.js
Expand Up @@ -212,7 +212,7 @@ var Route = React.createClass({
},

render: function () {
return this.props.handler(this.state.handlerProps);
return this.props.handler(computeHandlerProps(this.state.matches || [], this.state.query));
}

});
Expand Down Expand Up @@ -341,7 +341,7 @@ function syncWithTransition(route, transition) {
toMatches = nextMatches;
}

return checkTransitionFromHooks(fromMatches, transition).then(function () {
return checkTransitionFromHooks(fromMatches, route.refs, transition).then(function () {
if (transition.isCancelled)
return; // No need to continue.

Expand All @@ -355,7 +355,6 @@ function syncWithTransition(route, transition) {
var state = {
path: transition.path,
matches: nextMatches,
handlerProps: computeHandlerProps(nextMatches, query),
activeParams: params,
activeQuery: query,
activeRoutes: nextMatches.map(function (match) {
Expand All @@ -376,15 +375,15 @@ function syncWithTransition(route, transition) {
* the route's handler, so that the deepest nested handlers are called first.
* Returns a promise that resolves after the last handler.
*/
function checkTransitionFromHooks(matches, transition) {
function checkTransitionFromHooks(matches, refs, transition) {
var promise = Promise.resolve();

reversedArray(matches).forEach(function (match) {
promise = promise.then(function () {
var handler = match.route.props.handler;

if (!transition.isCancelled && handler.willTransitionFrom)
return handler.willTransitionFrom(transition, match.handlerInstance);
return handler.willTransitionFrom(transition, refs[match.refName]);
});
});

Expand Down Expand Up @@ -417,31 +416,33 @@ function checkTransitionToHooks(matches, transition) {
*/
function computeHandlerProps(matches, query) {
var props = {
ref: null,
key: null,
params: null,
query: null,
activeRoute: null
};

var previousMatch;
reversedArray(matches).forEach(function (match) {
var childDescriptor;
reversedArray(matches).forEach(function (match, index) {
var route = match.route;

props = Route.getUnreservedProps(route.props);

props.ref = 'route-' + index;
props.key = Path.injectParams(route.props.path, match.params);
props.params = match.params;
props.query = query;

if (previousMatch) {
props.activeRoute = previousMatch.handlerInstance;
if (childDescriptor) {
props.activeRoute = childDescriptor;
} else {
props.activeRoute = null;
}

match.handlerInstance = route.props.handler(props);
childDescriptor = route.props.handler(props);

previousMatch = match;
match.refName = props.ref;
});

return props;
Expand Down

1 comment on commit a64d0cd

@ryanflorence
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

love the variable names, I remember a few weeks ago when we were scratching our heads about descriptors v. instances

Please sign in to comment.